@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
@@ -0,0 +1,79 @@
1
+ import type { PayloadHandler } from "payload";
2
+ import type { ZodType } from "zod";
3
+ import { type CompactProposalRepairIssue } from "../../features/proposals/repair.js";
4
+ import type { ActionProposal } from "../../features/proposals/types.js";
5
+ import { type CollectionAction, type ResolvedCollectionPermissionMap } from "../../features/collectionPermissions.js";
6
+ import type { ToolFailure } from "./types.js";
7
+ type ToolDefinition<Input> = {
8
+ description: string;
9
+ execute: (input: Input) => unknown | Promise<unknown>;
10
+ inputSchema: ZodType;
11
+ };
12
+ type ToolTarget = {
13
+ collection?: string;
14
+ id?: string;
15
+ slug?: string;
16
+ };
17
+ type ToolErrorInput = ToolFailure & {
18
+ id?: string;
19
+ };
20
+ type RepairableToolErrorInput = ToolErrorInput & {
21
+ issues: CompactProposalRepairIssue[];
22
+ };
23
+ type CollectionToolOptions<Input extends {
24
+ collection: string;
25
+ }> = ToolDefinition<Input> & {
26
+ action?: CollectionAction;
27
+ currentDocumentOnly?: boolean;
28
+ getDocumentID?: (input: Input) => string | undefined;
29
+ name: string;
30
+ };
31
+ type ProposalToolOptions<Input> = ToolDefinition<Input> & {
32
+ beforeRepair?: (input: Input) => unknown | null;
33
+ getRepairTarget: (input: Input) => ToolTarget;
34
+ name: string;
35
+ };
36
+ export type ChatToolFactoryOptions = {
37
+ collections?: ResolvedCollectionPermissionMap;
38
+ currentDocument?: {
39
+ collection: string;
40
+ id: string;
41
+ };
42
+ debug: Record<string, unknown>;
43
+ prompt: string;
44
+ req: Parameters<PayloadHandler>[0];
45
+ };
46
+ export declare const createChatToolFactory: ({ collections, currentDocument, debug, prompt, req }: ChatToolFactoryOptions) => {
47
+ addProposal: <Proposal extends ActionProposal>(proposal: Proposal) => {
48
+ error: string;
49
+ errorCode: string;
50
+ retryable: boolean;
51
+ } | (Proposal & {
52
+ _aiSignature: import("../../features/proposals/types.js").AIActionSignature;
53
+ });
54
+ collectionTool: <Input extends {
55
+ collection: string;
56
+ }>({ action, currentDocumentOnly, description, execute, getDocumentID, inputSchema, name, }: CollectionToolOptions<Input>) => ToolDefinition<Input>;
57
+ error: ({ collection, details, errorCode, message, slug, tool }: ToolErrorInput) => {
58
+ error: string;
59
+ errorCode: string;
60
+ retryable: boolean;
61
+ };
62
+ proposalCollectionTool: <Input extends {
63
+ collection: string;
64
+ }>(options: CollectionToolOptions<Input> & Pick<ProposalToolOptions<Input>, "beforeRepair" | "getRepairTarget">) => ToolDefinition<Input>;
65
+ proposalTool: <Input>({ beforeRepair, description, execute, getRepairTarget, inputSchema, name, }: ProposalToolOptions<Input>) => ToolDefinition<Input>;
66
+ proposals: ActionProposal[];
67
+ repairableError: ({ collection, details, id, issues, message, slug, tool }: RepairableToolErrorInput) => {
68
+ error: string;
69
+ errorCode: "REPAIR_EXHAUSTED" | "INVALID_PROPOSAL_DATA";
70
+ repair: {
71
+ attempt: number;
72
+ issues: CompactProposalRepairIssue[];
73
+ maxAttempts: number;
74
+ };
75
+ retryable: boolean;
76
+ };
77
+ toolFailures: ToolFailure[];
78
+ };
79
+ export {};
@@ -0,0 +1,156 @@
1
+ import { createProposalRepairTracker } from "../../features/proposals/repair.js";
2
+ import { signAIActionProposal } from "../../features/proposals/signing.js";
3
+ import { containsSensitiveData } from "../../features/sensitiveData.js";
4
+ import { isCollectionActionAllowed } from "../../features/collectionPermissions.js";
5
+ import { hasLocalizedData } from "../../utils/data.js";
6
+ import { getLogPreview, logHandlerEvent } from "../../utils/logging.js";
7
+ import { getProposalSummary } from "./proposalTools.js";
8
+ export const createChatToolFactory = ({ collections, currentDocument, debug, prompt, req })=>{
9
+ const proposals = [];
10
+ const toolFailures = [];
11
+ const proposalRepairTracker = createProposalRepairTracker();
12
+ const registerToolFailure = (failure)=>{
13
+ toolFailures.push(failure);
14
+ logHandlerEvent(req, "warn", {
15
+ debug,
16
+ ...failure,
17
+ msg: "AI tool validation failed",
18
+ promptPreview: getLogPreview(prompt)
19
+ });
20
+ };
21
+ const error = ({ collection, details, errorCode = "NON_RETRYABLE_TOOL_ERROR", message, slug, tool })=>{
22
+ registerToolFailure({
23
+ collection,
24
+ details,
25
+ errorCode,
26
+ message,
27
+ retryable: false,
28
+ slug,
29
+ tool
30
+ });
31
+ return {
32
+ error: message,
33
+ errorCode,
34
+ retryable: false
35
+ };
36
+ };
37
+ const repairableError = ({ collection, details, id, issues, message, slug, tool })=>{
38
+ const repair = proposalRepairTracker.registerFailure({
39
+ collection,
40
+ id,
41
+ slug,
42
+ tool
43
+ });
44
+ const responseMessage = repair.retryable ? message : "The single proposal repair attempt failed. Do not call the same proposal tool for this target again.";
45
+ registerToolFailure({
46
+ collection,
47
+ details,
48
+ errorCode: repair.errorCode,
49
+ message: responseMessage,
50
+ retryable: repair.retryable,
51
+ slug,
52
+ tool
53
+ });
54
+ return {
55
+ error: responseMessage,
56
+ errorCode: repair.errorCode,
57
+ repair: {
58
+ attempt: repair.attempt,
59
+ issues,
60
+ maxAttempts: repair.maxAttempts
61
+ },
62
+ retryable: repair.retryable
63
+ };
64
+ };
65
+ const getRepairLimitError = ({ collection, id, slug, tool })=>{
66
+ const callState = proposalRepairTracker.beginCall({
67
+ collection,
68
+ id,
69
+ slug,
70
+ tool
71
+ });
72
+ if (callState !== "blocked") return null;
73
+ return error({
74
+ collection,
75
+ errorCode: "REPAIR_EXHAUSTED",
76
+ message: "The single proposal repair attempt was already used. Do not call this proposal tool for the same target again.",
77
+ slug,
78
+ tool
79
+ });
80
+ };
81
+ const addProposal = (proposal)=>{
82
+ const containsSensitiveProposalData = "data" in proposal && proposal.data && containsSensitiveData(proposal.data) || "localizedData" in proposal && hasLocalizedData(proposal.localizedData) && Object.values(proposal.localizedData).some((value)=>containsSensitiveData(value));
83
+ if (containsSensitiveProposalData) {
84
+ return error({
85
+ details: getProposalSummary(proposal),
86
+ message: "Proposal contains sensitive fields and cannot be created.",
87
+ tool: `propose${proposal.action[0]?.toUpperCase()}${proposal.action.slice(1)}`
88
+ });
89
+ }
90
+ const signedProposal = signAIActionProposal(proposal);
91
+ proposals.push(signedProposal);
92
+ logHandlerEvent(req, "info", {
93
+ debug,
94
+ msg: "AI proposal created",
95
+ proposal: getProposalSummary(signedProposal)
96
+ });
97
+ return signedProposal;
98
+ };
99
+ const collectionTool = ({ action, currentDocumentOnly, description, execute, getDocumentID, inputSchema, name })=>({
100
+ description,
101
+ inputSchema,
102
+ execute: async (input)=>{
103
+ if (currentDocumentOnly) {
104
+ const id = getDocumentID?.(input);
105
+ if (!currentDocument || input.collection !== currentDocument.collection || id !== currentDocument.id) {
106
+ return error({
107
+ collection: input.collection,
108
+ message: `Only the current document can be ${action === "read" ? "read" : `${action}d`} in this context.`,
109
+ tool: name
110
+ });
111
+ }
112
+ }
113
+ if (action && !isCollectionActionAllowed({
114
+ action,
115
+ permissions: collections,
116
+ req,
117
+ slug: input.collection
118
+ })) {
119
+ return error({
120
+ collection: input.collection,
121
+ message: `${action} is not enabled for collection: ${input.collection}`,
122
+ tool: "collectionPermissionCheck"
123
+ });
124
+ }
125
+ return execute(input);
126
+ }
127
+ });
128
+ const proposalTool = ({ beforeRepair, description, execute, getRepairTarget, inputSchema, name })=>({
129
+ description,
130
+ inputSchema,
131
+ execute: async (input)=>{
132
+ const validationError = beforeRepair?.(input);
133
+ if (validationError) return validationError;
134
+ const repairLimitError = getRepairLimitError({
135
+ ...getRepairTarget(input),
136
+ tool: name
137
+ });
138
+ if (repairLimitError) return repairLimitError;
139
+ return execute(input);
140
+ }
141
+ });
142
+ const proposalCollectionTool = (options)=>collectionTool({
143
+ ...options,
144
+ ...proposalTool(options)
145
+ });
146
+ return {
147
+ addProposal,
148
+ collectionTool,
149
+ error,
150
+ proposalCollectionTool,
151
+ proposalTool,
152
+ proposals,
153
+ repairableError,
154
+ toolFailures
155
+ };
156
+ };
@@ -0,0 +1,57 @@
1
+ import type { AIRequestOptions } from "../../features/providers/requestContext.js";
2
+ import type { ResolvedCollectionPermissionMap } from "../../features/collectionPermissions.js";
3
+ import type { ChatMention } from "../../features/schema/context.js";
4
+ export type ChatMediaAttachment = {
5
+ collection?: string;
6
+ filename?: string;
7
+ filesize?: number;
8
+ id?: string;
9
+ mimeType?: string;
10
+ type?: "media";
11
+ url?: string;
12
+ };
13
+ export type ChatBody = {
14
+ attachments?: ChatMediaAttachment[];
15
+ documentScope?: {
16
+ collection?: string;
17
+ id?: string;
18
+ type?: "collection";
19
+ } | {
20
+ slug?: string;
21
+ type?: "global";
22
+ };
23
+ mentions?: ChatMention[];
24
+ model?: string;
25
+ prompt?: string;
26
+ provider?: string;
27
+ };
28
+ export type ChatDebug = {
29
+ model: string;
30
+ provider: string;
31
+ tools: string[];
32
+ };
33
+ export type ToolFailure = {
34
+ collection?: string;
35
+ details?: Record<string, unknown>;
36
+ errorCode?: string;
37
+ message: string;
38
+ retryable?: boolean;
39
+ slug?: string;
40
+ tool: string;
41
+ };
42
+ export type TokenUsage = {
43
+ inputTokens?: number;
44
+ outputTokens?: number;
45
+ totalTokens?: number;
46
+ };
47
+ export type ChatOptions = AIRequestOptions & {
48
+ collections?: ResolvedCollectionPermissionMap;
49
+ maxOutputTokens?: number;
50
+ promptCaching?: boolean;
51
+ };
52
+ export type ChatIntent = "create" | "delete" | "read" | "search" | "update" | "updateGlobal";
53
+ export type ProposalToolName = "proposeCreateDoc" | "proposeDeleteDoc" | "proposeUpdateDoc" | "proposeUpdateGlobal";
54
+ export type ToolChoice = {
55
+ toolName: ProposalToolName;
56
+ type: "tool";
57
+ };
@@ -0,0 +1 @@
1
+ export { };
@@ -1,46 +1,3 @@
1
1
  import type { PayloadHandler } from "payload";
2
- import { type AIActionSignature } from "../ai/proposalSigning.js";
3
- import { type AIModelConfig, type ResolvedAIProviderConfig } from "../ai/providerOptions.js";
4
- import { type ResolvedMaxTokenUsageOptions } from "../ai/tokenUsage.js";
5
- import { type ResolvedCollectionPermissionMap } from "../payload/collectionPermissions.js";
6
- type LocalizedDataInput = Record<string, Record<string, unknown>>;
7
- type ProposalWritePayload = {
8
- data: Record<string, unknown>;
9
- localizedData?: never;
10
- } | {
11
- data?: never;
12
- localizedData: LocalizedDataInput;
13
- };
14
- export type ActionProposal = (({
15
- action: "create";
16
- collection: string;
17
- label: string;
18
- } & ProposalWritePayload) | {
19
- action: "delete";
20
- collection: string;
21
- id: string;
22
- label: string;
23
- } | ({
24
- action: "update";
25
- collection: string;
26
- id: string;
27
- label: string;
28
- } & ProposalWritePayload) | ({
29
- action: "updateGlobal";
30
- label: string;
31
- slug: string;
32
- } & ProposalWritePayload)) & {
33
- _aiSignature?: AIActionSignature;
34
- locale?: string;
35
- };
36
- type ChatOptions = {
37
- allowUserApiKeys?: boolean;
38
- collections?: ResolvedCollectionPermissionMap;
39
- maxOutputTokens?: number;
40
- maxTokenUsage?: ResolvedMaxTokenUsageOptions;
41
- models?: AIModelConfig;
42
- promptCaching?: boolean;
43
- providers?: ResolvedAIProviderConfig[];
44
- };
2
+ import type { ChatOptions } from "./chat/types.js";
45
3
  export declare const createChatHandler: (options?: ChatOptions) => PayloadHandler;
46
- export {};