@mvriu5/payload-ai 1.6.2 → 1.6.4

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.
Files changed (126) hide show
  1. package/dist/components/action-toast/ActionToast.d.ts +1 -14
  2. package/dist/components/action-toast/ActionToast.js +1 -1
  3. package/dist/components/ai-input/AIInput.js +1 -1
  4. package/dist/components/audit-log-list/AuditLogList.d.ts +2 -3
  5. package/dist/components/diff-dialog/DiffDialog.d.ts +2 -6
  6. package/dist/components/diff-dialog/DiffDialog.js +1 -1
  7. package/dist/components/generate-field/GenerateField.js +2 -0
  8. package/dist/components/hooks/useAIChatStream.d.ts +1 -1
  9. package/dist/components/hooks/useAISettings.d.ts +1 -1
  10. package/dist/components/hooks/useAISettings.js +1 -1
  11. package/dist/components/hooks/useDocumentMentionSuggestions.js +1 -1
  12. package/dist/components/hooks/useMentions.js +1 -1
  13. package/dist/components/hooks/usePluginConfig.d.ts +6 -6
  14. package/dist/components/hooks/usePluginConfig.js +1 -1
  15. package/dist/{payload → features}/collectionPermissions.js +1 -1
  16. package/dist/{payload/textFieldGeneration.d.ts → features/content/fieldGeneration.d.ts} +4 -0
  17. package/dist/{payload/textFieldGeneration.js → features/content/fieldGeneration.js} +19 -3
  18. package/dist/{ai → features}/promptCaching.d.ts +2 -2
  19. package/dist/features/proposals/containerNormalization.d.ts +8 -0
  20. package/dist/features/proposals/containerNormalization.js +112 -0
  21. package/dist/{payload/proposalData.d.ts → features/proposals/data.d.ts} +5 -10
  22. package/dist/features/proposals/data.js +113 -0
  23. package/dist/features/proposals/fieldNormalization.d.ts +2 -0
  24. package/dist/features/proposals/fieldNormalization.js +69 -0
  25. package/dist/features/proposals/normalizationTypes.d.ts +31 -0
  26. package/dist/features/proposals/normalizationTypes.js +1 -0
  27. package/dist/features/proposals/optionInference.d.ts +13 -0
  28. package/dist/features/proposals/optionInference.js +46 -0
  29. package/dist/features/proposals/recordNormalization.d.ts +6 -0
  30. package/dist/features/proposals/recordNormalization.js +78 -0
  31. package/dist/features/proposals/relationshipNormalization.d.ts +8 -0
  32. package/dist/features/proposals/relationshipNormalization.js +71 -0
  33. package/dist/{ai/proposalRepair.d.ts → features/proposals/repair.d.ts} +1 -1
  34. package/dist/{ai/proposalSigning.d.ts → features/proposals/signing.d.ts} +2 -5
  35. package/dist/{ai/proposalSigning.js → features/proposals/signing.js} +1 -1
  36. package/dist/features/proposals/target.d.ts +52 -0
  37. package/dist/features/proposals/target.js +125 -0
  38. package/dist/features/proposals/types.d.ts +49 -0
  39. package/dist/features/proposals/types.js +1 -0
  40. package/dist/features/proposals/validation.d.ts +12 -0
  41. package/dist/features/proposals/validation.js +26 -0
  42. package/dist/features/providers/requestContext.d.ts +53 -0
  43. package/dist/features/providers/requestContext.js +90 -0
  44. package/dist/{ai/providerRuntime.d.ts → features/providers/runtime.d.ts} +1 -1
  45. package/dist/{ai/providerRuntime.js → features/providers/runtime.js} +1 -1
  46. package/dist/features/schema/context.d.ts +7 -0
  47. package/dist/features/schema/context.js +11 -0
  48. package/dist/{payload/schemaContext.d.ts → features/schema/description.d.ts} +6 -33
  49. package/dist/{payload/schemaContext.js → features/schema/description.js} +3 -207
  50. package/dist/{payload/toolFieldSelection.d.ts → features/schema/fieldSelection.d.ts} +1 -1
  51. package/dist/{payload/toolFieldSelection.js → features/schema/fieldSelection.js} +2 -2
  52. package/dist/features/schema/mentionDocuments.d.ts +15 -0
  53. package/dist/features/schema/mentionDocuments.js +50 -0
  54. package/dist/features/schema/mentions.d.ts +18 -0
  55. package/dist/features/schema/mentions.js +100 -0
  56. package/dist/{payload/normalizeData.js → features/schema/normalize.js} +1 -1
  57. package/dist/features/schema/registry.d.ts +30 -0
  58. package/dist/features/schema/registry.js +73 -0
  59. package/dist/{payload → features/schema}/toolSchemas.d.ts +1 -1
  60. package/dist/{payload → features/schema}/toolSchemas.js +1 -1
  61. package/dist/{ai → features}/sensitiveData.js +1 -1
  62. package/dist/{ai → features}/tokenUsage.d.ts +1 -2
  63. package/dist/handlers/apply/audit.d.ts +42 -0
  64. package/dist/handlers/apply/audit.js +150 -0
  65. package/dist/handlers/apply/operations.d.ts +18 -0
  66. package/dist/handlers/apply/operations.js +377 -0
  67. package/dist/handlers/apply/types.d.ts +33 -0
  68. package/dist/handlers/apply/types.js +1 -0
  69. package/dist/handlers/apply/validation.d.ts +45 -0
  70. package/dist/handlers/apply/validation.js +125 -0
  71. package/dist/handlers/applyActionHandler.d.ts +1 -6
  72. package/dist/handlers/applyActionHandler.js +13 -757
  73. package/dist/handlers/auditLogHandler.js +4 -8
  74. package/dist/handlers/chat/intent.d.ts +32 -0
  75. package/dist/handlers/chat/intent.js +155 -0
  76. package/dist/handlers/chat/prompt.d.ts +20 -0
  77. package/dist/handlers/chat/prompt.js +55 -0
  78. package/dist/handlers/chat/proposalTools.d.ts +96 -0
  79. package/dist/handlers/chat/proposalTools.js +451 -0
  80. package/dist/handlers/chat/streaming.d.ts +38 -0
  81. package/dist/handlers/chat/streaming.js +136 -0
  82. package/dist/handlers/chat/toolFactory.d.ts +79 -0
  83. package/dist/handlers/chat/toolFactory.js +156 -0
  84. package/dist/handlers/chat/types.d.ts +57 -0
  85. package/dist/handlers/chat/types.js +1 -0
  86. package/dist/handlers/chatHandler.d.ts +1 -44
  87. package/dist/handlers/chatHandler.js +186 -1162
  88. package/dist/handlers/generateFieldHandler.d.ts +3 -8
  89. package/dist/handlers/generateFieldHandler.js +71 -99
  90. package/dist/handlers/http.d.ts +17 -0
  91. package/dist/handlers/http.js +21 -0
  92. package/dist/handlers/mediaUploadHandler.js +12 -34
  93. package/dist/handlers/mentionSuggestionHandler.d.ts +1 -1
  94. package/dist/handlers/mentionSuggestionHandler.js +6 -10
  95. package/dist/handlers/proposalDiffHandler.d.ts +1 -1
  96. package/dist/handlers/proposalDiffHandler.js +72 -195
  97. package/dist/handlers/translateDocumentHandler.d.ts +3 -8
  98. package/dist/handlers/translateDocumentHandler.js +26 -82
  99. package/dist/index.d.ts +4 -30
  100. package/dist/index.js +31 -421
  101. package/dist/plugin/admin.d.ts +30 -0
  102. package/dist/plugin/admin.js +155 -0
  103. package/dist/plugin/collections.d.ts +3 -0
  104. package/dist/plugin/collections.js +169 -0
  105. package/dist/plugin/endpoints.d.ts +23 -0
  106. package/dist/plugin/endpoints.js +84 -0
  107. package/dist/plugin/options.d.ts +4 -0
  108. package/dist/plugin/options.js +13 -0
  109. package/dist/plugin/types.d.ts +28 -0
  110. package/dist/plugin/types.js +1 -0
  111. package/dist/{payload/shared.d.ts → utils/data.d.ts} +2 -12
  112. package/package.json +1 -1
  113. package/dist/payload/proposalData.js +0 -578
  114. /package/dist/{payload → features}/collectionPermissions.d.ts +0 -0
  115. /package/dist/{payload → features/content}/documentTranslation.d.ts +0 -0
  116. /package/dist/{payload → features/content}/documentTranslation.js +0 -0
  117. /package/dist/{ai → features}/promptCaching.js +0 -0
  118. /package/dist/{ai/proposalRepair.js → features/proposals/repair.js} +0 -0
  119. /package/dist/{ai/providerOptions.d.ts → features/providers/options.d.ts} +0 -0
  120. /package/dist/{ai/providerOptions.js → features/providers/options.js} +0 -0
  121. /package/dist/{payload/normalizeData.d.ts → features/schema/normalize.d.ts} +0 -0
  122. /package/dist/{ai → features}/sensitiveData.d.ts +0 -0
  123. /package/dist/{ai → features}/tokenUsage.js +0 -0
  124. /package/dist/{payload/shared.js → utils/data.js} +0 -0
  125. /package/dist/{payload → utils}/logging.d.ts +0 -0
  126. /package/dist/{payload → utils}/logging.js +0 -0
@@ -1,5 +1,5 @@
1
1
  import type { LanguageModel } from "ai";
2
- import { type AIProvider, type AIModelConfig } from "./providerOptions.js";
2
+ import { type AIProvider, type AIModelConfig } from "./options.js";
3
3
  type ProviderConfig = {
4
4
  apiKey?: string | null;
5
5
  defaultModels?: AIModelConfig["defaults"];
@@ -1,4 +1,4 @@
1
- import { defaultAIModels } from "./providerOptions.js";
1
+ import { defaultAIModels } from "./options.js";
2
2
  export const getProviderConfig = ({ apiKey, defaultModels, model, provider })=>{
3
3
  const defaultModel = defaultModels?.[provider] || defaultAIModels[provider];
4
4
  if (provider === "claude") {
@@ -0,0 +1,7 @@
1
+ export { collectBlocks, describeCollectionLikeConfig, describeCollectionLikeSummary, type CollectionLikeConfig, type FieldConfig } from "./description.js";
2
+ export { getAllowedCollectionSlugs, getMentionContext, type ChatMention } from "./mentions.js";
3
+ export { createSchemaContextRegistry, type SchemaContextRegistry } from "./registry.js";
4
+ export declare const buildPromptWithMentionContext: ({ mentionContext, prompt }: {
5
+ mentionContext: Record<string, unknown>[];
6
+ prompt: string;
7
+ }) => string;
@@ -0,0 +1,11 @@
1
+ export { collectBlocks, describeCollectionLikeConfig, describeCollectionLikeSummary } from "./description.js";
2
+ export { getAllowedCollectionSlugs, getMentionContext } from "./mentions.js";
3
+ export { createSchemaContextRegistry } from "./registry.js";
4
+ export const buildPromptWithMentionContext = ({ mentionContext, prompt })=>{
5
+ if (mentionContext.length === 0) return prompt;
6
+ return [
7
+ "Selected Payload references JSON. Treat inline reference labels as these entities:",
8
+ JSON.stringify(mentionContext),
9
+ prompt
10
+ ].join("\n\n");
11
+ };
@@ -1,14 +1,5 @@
1
- import type { PayloadHandler } from "payload";
2
- import { type ResolvedCollectionPermissionMap } from "./collectionPermissions.js";
3
- import { type CollectionConfig as NormalizeCollectionConfig } from "./normalizeData.js";
4
- export type ChatMention = {
5
- collection?: string;
6
- id?: string;
7
- label?: string;
8
- parent?: string;
9
- slug?: string;
10
- type?: "block" | "collection" | "doc" | "global" | "locale";
11
- };
1
+ import { type ResolvedCollectionPermissionMap } from "../collectionPermissions.js";
2
+ import { type CollectionConfig as NormalizeCollectionConfig } from "./normalize.js";
12
3
  export type FieldConfig = {
13
4
  admin?: Record<string, unknown>;
14
5
  blocks?: BlockConfig[];
@@ -26,7 +17,7 @@ export type FieldConfig = {
26
17
  required?: boolean;
27
18
  type?: string;
28
19
  };
29
- type BlockConfig = {
20
+ export type BlockConfig = {
30
21
  fields?: FieldConfig[];
31
22
  labels?: {
32
23
  plural?: unknown;
@@ -34,7 +25,7 @@ type BlockConfig = {
34
25
  };
35
26
  slug: string;
36
27
  };
37
- type CollectionLikeConfig = {
28
+ export type CollectionLikeConfig = {
38
29
  admin?: {
39
30
  useAsTitle?: string;
40
31
  };
@@ -49,24 +40,13 @@ type CollectionLikeConfig = {
49
40
  slug: string;
50
41
  versions?: NormalizeCollectionConfig["versions"];
51
42
  };
52
- type MentionContext = {
53
- blockContexts: (Record<string, unknown> & {
54
- parent: string;
55
- slug: string;
56
- })[];
57
- collectionSlugs: string[];
58
- collections?: ResolvedCollectionPermissionMap;
59
- globalSlugs: string[];
60
- mentions?: ChatMention[];
61
- req: Parameters<PayloadHandler>[0];
62
- };
63
43
  export declare const describeCollectionLikeSummary: ({ config, permissions, type, }: {
64
44
  config: CollectionLikeConfig;
65
45
  permissions?: ResolvedCollectionPermissionMap;
66
46
  type: "collection" | "global";
67
47
  }) => {
68
48
  access: {
69
- aiPermissions: Record<import("./collectionPermissions.js").CollectionAction, boolean> | {
49
+ aiPermissions: Record<import("../collectionPermissions.js").CollectionAction, boolean> | {
70
50
  read: boolean;
71
51
  update: boolean;
72
52
  };
@@ -88,7 +68,7 @@ export declare const describeCollectionLikeConfig: ({ config, permissions, type,
88
68
  }) => {
89
69
  fields: Record<string, unknown>[];
90
70
  access: {
91
- aiPermissions: Record<import("./collectionPermissions.js").CollectionAction, boolean> | {
71
+ aiPermissions: Record<import("../collectionPermissions.js").CollectionAction, boolean> | {
92
72
  read: boolean;
93
73
  update: boolean;
94
74
  };
@@ -110,10 +90,3 @@ export declare const collectBlocks: ({ fields, parent }: {
110
90
  parent: string;
111
91
  slug: string;
112
92
  })[];
113
- export declare const getAllowedCollectionSlugs: (req: Parameters<PayloadHandler>[0], collections?: ResolvedCollectionPermissionMap) => string[];
114
- export declare const getMentionContext: ({ blockContexts, collectionSlugs, collections, globalSlugs, mentions, req }: MentionContext) => Promise<Record<string, unknown>[]>;
115
- export declare const buildPromptWithMentionContext: ({ mentionContext, prompt }: {
116
- mentionContext: Record<string, unknown>[];
117
- prompt: string;
118
- }) => string;
119
- export {};
@@ -1,6 +1,6 @@
1
- import { getCollectionPermissions, getCollectionSlugsForAction } from "./collectionPermissions.js";
2
- import { getSchemaFields as getNormalizeSchemaFields, hasDrafts, isAuthCollection } from "./normalizeData.js";
3
- import { getSerializableLabel, isInternalCollection, isRecord } from "./shared.js";
1
+ import { getCollectionPermissions } from "../collectionPermissions.js";
2
+ import { getSchemaFields as getNormalizeSchemaFields, hasDrafts, isAuthCollection } from "./normalize.js";
3
+ import { getSerializableLabel, isRecord } from "../../utils/data.js";
4
4
  const getSerializableRelationTo = (relationTo)=>{
5
5
  if (typeof relationTo === "string") return relationTo;
6
6
  if (Array.isArray(relationTo) && relationTo.every((item)=>typeof item === "string")) {
@@ -11,37 +11,6 @@ const getSerializableRelationTo = (relationTo)=>{
11
11
  const fieldDescriptionCache = new WeakMap();
12
12
  const blockDescriptionCache = new WeakMap();
13
13
  const collectionDescriptionCache = new WeakMap();
14
- const getLocaleConfigs = (req)=>{
15
- const localization = req.payload.config.localization;
16
- const defaultLocale = localization?.defaultLocale;
17
- const locales = localization?.locales || [];
18
- return locales.flatMap((locale)=>{
19
- if (typeof locale === "string") {
20
- return [
21
- {
22
- code: locale,
23
- ...locale === defaultLocale ? {
24
- isDefault: true
25
- } : {},
26
- label: locale
27
- }
28
- ];
29
- }
30
- if (!isRecord(locale)) return [];
31
- const code = "code" in locale && typeof locale.code === "string" ? locale.code : "label" in locale && typeof locale.label === "string" ? locale.label : undefined;
32
- if (!code) return [];
33
- const label = "label" in locale ? getSerializableLabel(locale.label, code) : code;
34
- return [
35
- {
36
- code,
37
- ...code === defaultLocale ? {
38
- isDefault: true
39
- } : {},
40
- label
41
- }
42
- ];
43
- });
44
- };
45
14
  const getSerializableOptions = (options)=>{
46
15
  if (!Array.isArray(options)) return undefined;
47
16
  const serializedOptions = options.flatMap((option)=>{
@@ -280,176 +249,3 @@ export const collectBlocks = ({ fields, parent })=>{
280
249
  }
281
250
  return blocks;
282
251
  };
283
- export const getAllowedCollectionSlugs = (req, collections)=>{
284
- return getCollectionSlugsForAction({
285
- action: "read",
286
- permissions: collections,
287
- req
288
- });
289
- };
290
- export const getMentionContext = async ({ blockContexts, collectionSlugs, collections, globalSlugs, mentions, req })=>{
291
- if (!mentions || mentions.length === 0) return [];
292
- const context = [];
293
- const seen = new Set();
294
- const localeConfigs = getLocaleConfigs(req);
295
- const localeConfigsByCode = new Map(localeConfigs.map((locale)=>[
296
- locale.code,
297
- locale
298
- ]));
299
- const collectionSlugSet = new Set(collectionSlugs);
300
- const globalSlugSet = new Set(globalSlugs);
301
- const collectionConfigsBySlug = new Map(req.payload.config.collections.map((collection)=>[
302
- collection.slug,
303
- collection
304
- ]));
305
- const globalConfigsBySlug = new Map(req.payload.config.globals?.map((global)=>[
306
- global.slug,
307
- global
308
- ]));
309
- const blockContextsBySlug = new Map();
310
- for (const block of blockContexts){
311
- const blocks = blockContextsBySlug.get(block.slug) || [];
312
- blocks.push(block);
313
- blockContextsBySlug.set(block.slug, blocks);
314
- }
315
- const selectedLocales = [];
316
- const selectedLocaleSet = new Set();
317
- for (const mention of mentions){
318
- if (mention.type !== "locale" || !mention.slug || selectedLocaleSet.has(mention.slug)) continue;
319
- selectedLocaleSet.add(mention.slug);
320
- selectedLocales.push(mention.slug);
321
- }
322
- const activeLocale = selectedLocales.at(-1);
323
- const mentionDocResults = await Promise.all(mentions.slice(0, 8).map(async (mention)=>{
324
- if (mention.type === "global" && mention.slug) {
325
- const slug = mention.slug;
326
- if (!globalSlugSet.has(slug)) return null;
327
- const globalConfig = globalConfigsBySlug.get(slug);
328
- if (!globalConfig) return null;
329
- return {
330
- doc: await req.payload.findGlobal({
331
- depth: 2,
332
- ...activeLocale ? {
333
- locale: activeLocale
334
- } : {},
335
- overrideAccess: false,
336
- req,
337
- slug: slug
338
- }).catch(()=>null),
339
- key: `global:${slug}`
340
- };
341
- }
342
- if (mention.type === "doc" && mention.collection && mention.id) {
343
- const slug = mention.collection;
344
- if (isInternalCollection(slug) || !collectionSlugSet.has(slug)) return null;
345
- return {
346
- doc: await req.payload.findByID({
347
- collection: slug,
348
- depth: 2,
349
- id: mention.id,
350
- ...activeLocale ? {
351
- locale: activeLocale
352
- } : {},
353
- overrideAccess: false,
354
- req
355
- }).catch(()=>null),
356
- key: `doc:${slug}:${mention.id}`
357
- };
358
- }
359
- return null;
360
- }));
361
- const docsByMentionKey = new Map();
362
- for (const result of mentionDocResults){
363
- if (result) docsByMentionKey.set(result.key, result.doc);
364
- }
365
- if (localeConfigs.length > 0) {
366
- context.push({
367
- activeLocale,
368
- defaultLocale: localeConfigs.find((locale)=>locale.isDefault)?.code,
369
- locales: localeConfigs,
370
- selectedLocales,
371
- type: "locales"
372
- });
373
- }
374
- for (const mention of mentions.slice(0, 8)){
375
- if (mention.type === "locale" && mention.slug) {
376
- const locale = localeConfigsByCode.get(mention.slug);
377
- if (!locale) continue;
378
- const key = `locale:${locale.code}`;
379
- if (seen.has(key)) continue;
380
- seen.add(key);
381
- context.push({
382
- ...locale,
383
- type: "locale"
384
- });
385
- }
386
- if (mention.type === "collection" && mention.slug) {
387
- const slug = mention.slug;
388
- const key = `collection:${slug}`;
389
- if (seen.has(key) || isInternalCollection(slug) || !collectionSlugSet.has(slug)) continue;
390
- const collectionConfig = collectionConfigsBySlug.get(slug);
391
- if (!collectionConfig) continue;
392
- seen.add(key);
393
- context.push(describeCollectionLikeConfig({
394
- config: collectionConfig,
395
- permissions: collections,
396
- type: "collection"
397
- }));
398
- }
399
- if (mention.type === "global" && mention.slug) {
400
- const slug = mention.slug;
401
- const key = `global:${slug}`;
402
- if (seen.has(key) || !globalSlugSet.has(slug)) continue;
403
- const globalConfig = globalConfigsBySlug.get(slug);
404
- if (!globalConfig) continue;
405
- const globalDoc = docsByMentionKey.get(key) ?? null;
406
- seen.add(key);
407
- context.push({
408
- ...describeCollectionLikeConfig({
409
- config: globalConfig,
410
- type: "global"
411
- }),
412
- doc: globalDoc
413
- });
414
- }
415
- if (mention.type === "block" && mention.slug) {
416
- const matchingBlocks = (blockContextsBySlug.get(mention.slug) || []).filter((block)=>!mention.parent || block.parent === mention.parent);
417
- for (const block of matchingBlocks){
418
- const key = `block:${block.parent}:${block.slug}`;
419
- if (seen.has(key)) continue;
420
- seen.add(key);
421
- context.push({
422
- ...block,
423
- type: "block"
424
- });
425
- }
426
- }
427
- if (mention.type === "doc" && mention.collection && mention.id) {
428
- const slug = mention.collection;
429
- const key = `doc:${slug}:${mention.id}`;
430
- if (seen.has(key) || isInternalCollection(slug) || !collectionSlugSet.has(slug)) continue;
431
- const doc = docsByMentionKey.get(key);
432
- if (!doc) continue;
433
- seen.add(key);
434
- context.push({
435
- collection: slug,
436
- doc,
437
- id: mention.id,
438
- label: mention.label || mention.id,
439
- ...activeLocale ? {
440
- locale: activeLocale
441
- } : {},
442
- type: "doc"
443
- });
444
- }
445
- }
446
- return context;
447
- };
448
- export const buildPromptWithMentionContext = ({ mentionContext, prompt })=>{
449
- if (mentionContext.length === 0) return prompt;
450
- return [
451
- "Selected Payload references JSON. Treat inline reference labels as these entities:",
452
- JSON.stringify(mentionContext),
453
- prompt
454
- ].join("\n\n");
455
- };
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { type CollectionConfig } from "./normalizeData.js";
2
+ import { type CollectionConfig } from "./normalize.js";
3
3
  export declare const maxToolSelectedFields = 12;
4
4
  export type ReadCollectionConfig = CollectionConfig & {
5
5
  admin?: {
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
- import { isSensitiveKey } from "../ai/sensitiveData.js";
3
- import { getSchemaFields } from "./normalizeData.js";
2
+ import { isSensitiveKey } from "../sensitiveData.js";
3
+ import { getSchemaFields } from "./normalize.js";
4
4
  export const maxToolSelectedFields = 12;
5
5
  const cheapFieldTypes = new Set([
6
6
  "checkbox",
@@ -0,0 +1,15 @@
1
+ import type { PayloadHandler } from "payload";
2
+ import type { SchemaContextRegistry } from "./registry.js";
3
+ type MentionDocumentReference = {
4
+ collection?: string;
5
+ id?: string;
6
+ slug?: string;
7
+ type?: string;
8
+ };
9
+ export declare const loadMentionDocuments: ({ activeLocale, mentions, registry, req, }: {
10
+ activeLocale?: string;
11
+ mentions: MentionDocumentReference[];
12
+ registry: Pick<SchemaContextRegistry, "collectionSlugSet" | "globalSlugSet">;
13
+ req: Parameters<PayloadHandler>[0];
14
+ }) => Promise<Map<string, unknown>>;
15
+ export {};
@@ -0,0 +1,50 @@
1
+ import { isInternalCollection } from "../../utils/data.js";
2
+ export const loadMentionDocuments = async ({ activeLocale, mentions, registry, req })=>{
3
+ const requests = new Map();
4
+ for (const mention of mentions){
5
+ if (mention.type === "global" && mention.slug && registry.globalSlugSet.has(mention.slug)) {
6
+ const request = {
7
+ key: `global:${mention.slug}`,
8
+ slug: mention.slug,
9
+ type: "global"
10
+ };
11
+ requests.set(request.key, request);
12
+ }
13
+ if (mention.type === "doc" && mention.collection && mention.id && !isInternalCollection(mention.collection) && registry.collectionSlugSet.has(mention.collection)) {
14
+ const request = {
15
+ collection: mention.collection,
16
+ id: mention.id,
17
+ key: `doc:${mention.collection}:${mention.id}`,
18
+ type: "doc"
19
+ };
20
+ requests.set(request.key, request);
21
+ }
22
+ }
23
+ const results = await Promise.all([
24
+ ...requests.values()
25
+ ].map(async (request)=>{
26
+ const doc = request.type === "global" ? await req.payload.findGlobal({
27
+ depth: 2,
28
+ ...activeLocale ? {
29
+ locale: activeLocale
30
+ } : {},
31
+ overrideAccess: false,
32
+ req,
33
+ slug: request.slug
34
+ }).catch(()=>null) : await req.payload.findByID({
35
+ collection: request.collection,
36
+ depth: 2,
37
+ id: request.id,
38
+ ...activeLocale ? {
39
+ locale: activeLocale
40
+ } : {},
41
+ overrideAccess: false,
42
+ req
43
+ }).catch(()=>null);
44
+ return [
45
+ request.key,
46
+ doc
47
+ ];
48
+ }));
49
+ return new Map(results);
50
+ };
@@ -0,0 +1,18 @@
1
+ import type { PayloadHandler } from "payload";
2
+ import { type ResolvedCollectionPermissionMap } from "../collectionPermissions.js";
3
+ import type { SchemaContextRegistry, SchemaRegistrySource } from "./registry.js";
4
+ export type ChatMention = {
5
+ collection?: string;
6
+ id?: string;
7
+ label?: string;
8
+ parent?: string;
9
+ slug?: string;
10
+ type?: "block" | "collection" | "doc" | "global" | "locale";
11
+ };
12
+ export declare const getAllowedCollectionSlugs: (req: Parameters<PayloadHandler>[0], collections?: ResolvedCollectionPermissionMap) => string[];
13
+ export declare const getMentionContext: <Collection extends SchemaRegistrySource, Global extends SchemaRegistrySource>({ collections, mentions, registry, req, }: {
14
+ collections?: ResolvedCollectionPermissionMap;
15
+ mentions?: ChatMention[];
16
+ registry: SchemaContextRegistry<Collection, Global>;
17
+ req: Parameters<PayloadHandler>[0];
18
+ }) => Promise<Record<string, unknown>[]>;
@@ -0,0 +1,100 @@
1
+ import { getCollectionSlugsForAction } from "../collectionPermissions.js";
2
+ import { isInternalCollection } from "../../utils/data.js";
3
+ import { describeCollectionLikeConfig } from "./description.js";
4
+ import { loadMentionDocuments } from "./mentionDocuments.js";
5
+ export const getAllowedCollectionSlugs = (req, collections)=>getCollectionSlugsForAction({
6
+ action: "read",
7
+ permissions: collections,
8
+ req
9
+ });
10
+ export const getMentionContext = async ({ collections, mentions, registry, req })=>{
11
+ if (!mentions?.length) return [];
12
+ const context = [];
13
+ const seen = new Set();
14
+ const selectedLocales = [
15
+ ...new Set(mentions.flatMap((mention)=>mention.type === "locale" && mention.slug ? [
16
+ mention.slug
17
+ ] : []))
18
+ ];
19
+ const activeLocale = selectedLocales.at(-1);
20
+ const limitedMentions = mentions.slice(0, 8);
21
+ const docsByMentionKey = await loadMentionDocuments({
22
+ activeLocale,
23
+ mentions: limitedMentions,
24
+ registry,
25
+ req
26
+ });
27
+ if (registry.localeConfigs.length > 0) {
28
+ context.push({
29
+ activeLocale,
30
+ defaultLocale: registry.localeConfigs.find((locale)=>locale.isDefault)?.code,
31
+ locales: registry.localeConfigs,
32
+ selectedLocales,
33
+ type: "locales"
34
+ });
35
+ }
36
+ for (const mention of limitedMentions){
37
+ if (mention.type === "locale" && mention.slug) {
38
+ const locale = registry.localeConfigsByCode.get(mention.slug);
39
+ if (!locale || seen.has(`locale:${locale.code}`)) continue;
40
+ seen.add(`locale:${locale.code}`);
41
+ context.push({
42
+ ...locale,
43
+ type: "locale"
44
+ });
45
+ }
46
+ if (mention.type === "collection" && mention.slug) {
47
+ const key = `collection:${mention.slug}`;
48
+ const config = registry.collectionConfigsBySlug.get(mention.slug);
49
+ if (seen.has(key) || isInternalCollection(mention.slug) || !config) continue;
50
+ seen.add(key);
51
+ context.push(describeCollectionLikeConfig({
52
+ config: config,
53
+ permissions: collections,
54
+ type: "collection"
55
+ }));
56
+ }
57
+ if (mention.type === "global" && mention.slug) {
58
+ const key = `global:${mention.slug}`;
59
+ const config = registry.globalConfigsBySlug.get(mention.slug);
60
+ if (seen.has(key) || !config) continue;
61
+ seen.add(key);
62
+ context.push({
63
+ ...describeCollectionLikeConfig({
64
+ config: config,
65
+ type: "global"
66
+ }),
67
+ doc: docsByMentionKey.get(key) ?? null
68
+ });
69
+ }
70
+ if (mention.type === "block" && mention.slug) {
71
+ const matchingBlocks = (registry.blocksBySlug.get(mention.slug) || []).filter((block)=>!mention.parent || block.parent === mention.parent);
72
+ for (const block of matchingBlocks){
73
+ const key = `block:${block.parent}:${block.slug}`;
74
+ if (seen.has(key)) continue;
75
+ seen.add(key);
76
+ context.push({
77
+ ...block,
78
+ type: "block"
79
+ });
80
+ }
81
+ }
82
+ if (mention.type === "doc" && mention.collection && mention.id) {
83
+ const key = `doc:${mention.collection}:${mention.id}`;
84
+ const doc = docsByMentionKey.get(key);
85
+ if (seen.has(key) || isInternalCollection(mention.collection) || !registry.collectionSlugSet.has(mention.collection) || !doc) continue;
86
+ seen.add(key);
87
+ context.push({
88
+ collection: mention.collection,
89
+ doc,
90
+ id: mention.id,
91
+ label: mention.label || mention.id,
92
+ ...activeLocale ? {
93
+ locale: activeLocale
94
+ } : {},
95
+ type: "doc"
96
+ });
97
+ }
98
+ }
99
+ return context;
100
+ };
@@ -1,4 +1,4 @@
1
- import { isRecord } from "./shared.js";
1
+ import { isRecord } from "../../utils/data.js";
2
2
  const SKIP_FIELD = Symbol("skipField");
3
3
  const getNamedFields = (fields)=>{
4
4
  return fields.filter((field)=>Boolean(field.name));
@@ -0,0 +1,30 @@
1
+ import type { PayloadHandler } from "payload";
2
+ import { type CollectionLikeConfig } from "./description.js";
3
+ export type LocaleConfig = {
4
+ code: string;
5
+ isDefault?: boolean;
6
+ label: string;
7
+ };
8
+ export type SchemaBlockContext = Record<string, unknown> & {
9
+ parent: string;
10
+ slug: string;
11
+ };
12
+ export type SchemaRegistrySource = {
13
+ fields?: unknown[];
14
+ slug: string;
15
+ };
16
+ export type SchemaContextRegistry<Collection extends SchemaRegistrySource = CollectionLikeConfig, Global extends SchemaRegistrySource = CollectionLikeConfig> = {
17
+ blocks: SchemaBlockContext[];
18
+ blocksBySlug: Map<string, SchemaBlockContext[]>;
19
+ collectionConfigsBySlug: Map<string, Collection>;
20
+ collectionSlugSet: Set<string>;
21
+ globalConfigsBySlug: Map<string, Global>;
22
+ globalSlugSet: Set<string>;
23
+ localeConfigs: LocaleConfig[];
24
+ localeConfigsByCode: Map<string, LocaleConfig>;
25
+ };
26
+ export declare const createSchemaContextRegistry: <Collection extends SchemaRegistrySource, Global extends SchemaRegistrySource>({ collections, globals, req, }: {
27
+ collections: Collection[];
28
+ globals: Global[];
29
+ req: Parameters<PayloadHandler>[0];
30
+ }) => SchemaContextRegistry<Collection, Global>;
@@ -0,0 +1,73 @@
1
+ import { getSerializableLabel, isRecord } from "../../utils/data.js";
2
+ import { collectBlocks } from "./description.js";
3
+ const getLocaleConfigs = (req)=>{
4
+ const localization = req.payload.config.localization;
5
+ const defaultLocale = localization?.defaultLocale;
6
+ return (localization?.locales || []).flatMap((locale)=>{
7
+ if (typeof locale === "string") {
8
+ return [
9
+ {
10
+ code: locale,
11
+ ...locale === defaultLocale ? {
12
+ isDefault: true
13
+ } : {},
14
+ label: locale
15
+ }
16
+ ];
17
+ }
18
+ if (!isRecord(locale)) return [];
19
+ const code = typeof locale.code === "string" ? locale.code : typeof locale.label === "string" ? locale.label : undefined;
20
+ if (!code) return [];
21
+ return [
22
+ {
23
+ code,
24
+ ...code === defaultLocale ? {
25
+ isDefault: true
26
+ } : {},
27
+ label: "label" in locale ? getSerializableLabel(locale.label, code) : code
28
+ }
29
+ ];
30
+ });
31
+ };
32
+ export const createSchemaContextRegistry = ({ collections, globals, req })=>{
33
+ const collectionConfigs = collections;
34
+ const globalConfigs = globals;
35
+ const blocks = [
36
+ ...collectionConfigs.flatMap((collection)=>collectBlocks({
37
+ fields: collection.fields || [],
38
+ parent: collection.slug
39
+ })),
40
+ ...globalConfigs.flatMap((global)=>collectBlocks({
41
+ fields: global.fields || [],
42
+ parent: global.slug
43
+ }))
44
+ ];
45
+ const blocksBySlug = new Map();
46
+ for (const block of blocks){
47
+ const matchingBlocks = blocksBySlug.get(block.slug) || [];
48
+ matchingBlocks.push(block);
49
+ blocksBySlug.set(block.slug, matchingBlocks);
50
+ }
51
+ const collectionConfigsBySlug = new Map(collections.map((collection)=>[
52
+ collection.slug,
53
+ collection
54
+ ]));
55
+ const globalConfigsBySlug = new Map(globals.map((global)=>[
56
+ global.slug,
57
+ global
58
+ ]));
59
+ const localeConfigs = getLocaleConfigs(req);
60
+ return {
61
+ blocks,
62
+ blocksBySlug,
63
+ collectionConfigsBySlug,
64
+ collectionSlugSet: new Set(collectionConfigsBySlug.keys()),
65
+ globalConfigsBySlug,
66
+ globalSlugSet: new Set(globalConfigsBySlug.keys()),
67
+ localeConfigs,
68
+ localeConfigsByCode: new Map(localeConfigs.map((locale)=>[
69
+ locale.code,
70
+ locale
71
+ ]))
72
+ };
73
+ };
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { type CollectionConfig } from "./normalizeData.js";
2
+ import { type CollectionConfig } from "./normalize.js";
3
3
  export declare const genericPayloadDataSchema: z.ZodRecord<z.ZodString, z.ZodUnknown>;
4
4
  export declare const createPayloadDataSchema: (config?: CollectionConfig | null) => z.ZodRecord<z.ZodString, z.ZodUnknown> | z.ZodObject<{
5
5
  [x: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
@@ -1,5 +1,5 @@
1
1
  import { z } from "zod";
2
- import { getSchemaFields } from "./normalizeData.js";
2
+ import { getSchemaFields } from "./normalize.js";
3
3
  const relationshipIDSchema = z.union([
4
4
  z.string().min(1),
5
5
  z.number()