@aws-amplify/data-schema 1.25.1 → 1.25.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/SchemaProcessor.js +13 -7
- package/dist/cjs/SchemaProcessor.js.map +1 -1
- package/dist/cjs/ai/ConversationSchemaProcessor.js +28 -4
- package/dist/cjs/ai/ConversationSchemaProcessor.js.map +1 -1
- package/dist/esm/SchemaProcessor.mjs +13 -7
- package/dist/esm/SchemaProcessor.mjs.map +1 -1
- package/dist/esm/ai/ConversationSchemaProcessor.mjs +28 -4
- package/dist/esm/ai/ConversationSchemaProcessor.mjs.map +1 -1
- package/dist/meta/cjs.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/SchemaProcessor.ts +17 -7
- package/src/ai/ConversationSchemaProcessor.ts +37 -4
|
@@ -2,7 +2,7 @@ import { accessData } from '../Authorization.mjs';
|
|
|
2
2
|
import { capitalize } from '../runtime/utils/stringTransformation.mjs';
|
|
3
3
|
|
|
4
4
|
const createConversationField = (typeDef, typeName) => {
|
|
5
|
-
const { aiModel, systemPrompt, handler, tools } = typeDef;
|
|
5
|
+
const { aiModel, systemPrompt, inferenceConfiguration, handler, tools } = typeDef;
|
|
6
6
|
const { strategy, provider } = extractAuthorization(typeDef, typeName);
|
|
7
7
|
const args = {
|
|
8
8
|
aiModel: aiModel.resourcePath,
|
|
@@ -17,9 +17,17 @@ const createConversationField = (typeDef, typeName) => {
|
|
|
17
17
|
// It doesn't affect non multi-line string inputs for system prompts
|
|
18
18
|
systemPrompt: systemPrompt.replace(/\r?\n/g, '\\n'),
|
|
19
19
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
// Add each arg with quotes (aiModel and systemPrompt)
|
|
21
|
+
const argsParts = [];
|
|
22
|
+
for (const [key, value] of Object.entries(args)) {
|
|
23
|
+
argsParts.push(`${key}: "${value}"`);
|
|
24
|
+
}
|
|
25
|
+
// Add inferenceConfiguration (do nothing if it doesn't exist or is empty)
|
|
26
|
+
const formattedInferenceConfig = formatInferenceConfig(inferenceConfiguration);
|
|
27
|
+
if (formattedInferenceConfig) {
|
|
28
|
+
argsParts.push(`inferenceConfiguration: ${formattedInferenceConfig}`);
|
|
29
|
+
}
|
|
30
|
+
const argsString = argsParts.join(', ');
|
|
23
31
|
const authString = `, auth: { strategy: ${strategy}, provider: ${provider} }`;
|
|
24
32
|
const functionHandler = {};
|
|
25
33
|
let handlerString = '';
|
|
@@ -36,6 +44,22 @@ const createConversationField = (typeDef, typeName) => {
|
|
|
36
44
|
const field = `${typeName}(conversationId: ID!, content: [AmplifyAIContentBlockInput], aiContext: AWSJSON, toolConfiguration: AmplifyAIToolConfigurationInput): AmplifyAIConversationMessage ${conversationDirective} @aws_cognito_user_pools`;
|
|
37
45
|
return { field, functionHandler };
|
|
38
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Format inferenceConfiguration as GraphQL input syntax.
|
|
49
|
+
* Returns null if config is undefined or has no valid properties.
|
|
50
|
+
*/
|
|
51
|
+
const formatInferenceConfig = (config) => {
|
|
52
|
+
if (!config)
|
|
53
|
+
return null;
|
|
54
|
+
const parts = [];
|
|
55
|
+
if (config.temperature !== undefined)
|
|
56
|
+
parts.push(`temperature: ${config.temperature}`);
|
|
57
|
+
if (config.maxTokens !== undefined)
|
|
58
|
+
parts.push(`maxTokens: ${config.maxTokens}`);
|
|
59
|
+
if (config.topP !== undefined)
|
|
60
|
+
parts.push(`topP: ${config.topP}`);
|
|
61
|
+
return parts.length > 0 ? `{ ${parts.join(', ')} }` : null;
|
|
62
|
+
};
|
|
39
63
|
const isRef = (query) => query?.data?.type === 'ref';
|
|
40
64
|
const extractAuthorization = (typeDef, typeName) => {
|
|
41
65
|
const { authorization } = typeDef.data;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ConversationSchemaProcessor.mjs","sources":["../../../src/ai/ConversationSchemaProcessor.ts"],"sourcesContent":["import { accessData } from '../Authorization';\nimport { capitalize } from '../runtime/utils';\nexport const createConversationField = (typeDef, typeName) => {\n const { aiModel, systemPrompt, handler, tools } = typeDef;\n const { strategy, provider } = extractAuthorization(typeDef, typeName);\n const args = {\n aiModel: aiModel.resourcePath,\n // This is done to escape newlines in potentially multi-line system prompts\n // e.g.\n // realtorChat: a.conversation({\n // aiModel: a.ai.model('Claude 3 Haiku'),\n // systemPrompt: `You are a helpful real estate assistant\n // Respond in the poetic form of haiku.`,\n // }),\n //\n // It doesn't affect non multi-line string inputs for system prompts\n systemPrompt: systemPrompt.replace(/\\r?\\n/g, '\\\\n'),\n };\n const
|
|
1
|
+
{"version":3,"file":"ConversationSchemaProcessor.mjs","sources":["../../../src/ai/ConversationSchemaProcessor.ts"],"sourcesContent":["import { accessData } from '../Authorization';\nimport { capitalize } from '../runtime/utils';\nexport const createConversationField = (typeDef, typeName) => {\n const { aiModel, systemPrompt, inferenceConfiguration, handler, tools } = typeDef;\n const { strategy, provider } = extractAuthorization(typeDef, typeName);\n const args = {\n aiModel: aiModel.resourcePath,\n // This is done to escape newlines in potentially multi-line system prompts\n // e.g.\n // realtorChat: a.conversation({\n // aiModel: a.ai.model('Claude 3 Haiku'),\n // systemPrompt: `You are a helpful real estate assistant\n // Respond in the poetic form of haiku.`,\n // }),\n //\n // It doesn't affect non multi-line string inputs for system prompts\n systemPrompt: systemPrompt.replace(/\\r?\\n/g, '\\\\n'),\n };\n // Add each arg with quotes (aiModel and systemPrompt)\n const argsParts = [];\n for (const [key, value] of Object.entries(args)) {\n argsParts.push(`${key}: \"${value}\"`);\n }\n // Add inferenceConfiguration (do nothing if it doesn't exist or is empty)\n const formattedInferenceConfig = formatInferenceConfig(inferenceConfiguration);\n if (formattedInferenceConfig) {\n argsParts.push(`inferenceConfiguration: ${formattedInferenceConfig}`);\n }\n const argsString = argsParts.join(', ');\n const authString = `, auth: { strategy: ${strategy}, provider: ${provider} }`;\n const functionHandler = {};\n let handlerString = '';\n if (handler) {\n const functionName = `Fn${capitalize(typeName)}`;\n const eventVersion = handler.eventVersion;\n handlerString = `, handler: { functionName: \"${functionName}\", eventVersion: \"${eventVersion}\" }`;\n functionHandler[functionName] = handler;\n }\n const toolsString = tools?.length\n ? `, tools: [${getConversationToolsString(tools)}]`\n : '';\n const conversationDirective = `@conversation(${argsString}${authString}${handlerString}${toolsString})`;\n const field = `${typeName}(conversationId: ID!, content: [AmplifyAIContentBlockInput], aiContext: AWSJSON, toolConfiguration: AmplifyAIToolConfigurationInput): AmplifyAIConversationMessage ${conversationDirective} @aws_cognito_user_pools`;\n return { field, functionHandler };\n};\n/**\n * Format inferenceConfiguration as GraphQL input syntax.\n * Returns null if config is undefined or has no valid properties.\n */\nconst formatInferenceConfig = (config) => {\n if (!config)\n return null;\n const parts = [];\n if (config.temperature !== undefined)\n parts.push(`temperature: ${config.temperature}`);\n if (config.maxTokens !== undefined)\n parts.push(`maxTokens: ${config.maxTokens}`);\n if (config.topP !== undefined)\n parts.push(`topP: ${config.topP}`);\n return parts.length > 0 ? `{ ${parts.join(', ')} }` : null;\n};\nconst isRef = (query) => query?.data?.type === 'ref';\nconst extractAuthorization = (typeDef, typeName) => {\n const { authorization } = typeDef.data;\n if (authorization.length === 0) {\n throw new Error(`Conversation ${typeName} is missing authorization rules. Use .authorization((allow) => allow.owner()) to configure authorization for your conversation route.`);\n }\n const { strategy, provider } = accessData(authorization[0]);\n if (strategy !== 'owner' || provider !== 'userPools') {\n throw new Error(`Conversation ${typeName} must use owner authorization with a user pool provider. Use .authorization((allow) => allow.owner()) to configure authorization for your conversation route.`);\n }\n return { strategy, provider };\n};\nconst getConversationToolsString = (tools) => tools\n .map((tool) => {\n const { name, description } = tool;\n // https://docs.aws.amazon.com/bedrock/latest/APIReference/API_runtime_ToolSpecification.html\n // Pattern: ^[a-zA-Z][a-zA-Z0-9_]*$\n // Length Constraints: Minimum length of 1. Maximum length of 64.\n const isValidToolName = /^[a-zA-Z][a-zA-Z0-9_]*$/.test(name) &&\n name.length >= 1 &&\n name.length <= 64;\n if (!isValidToolName) {\n throw new Error(`Tool name must be between 1 and 64 characters, start with a letter, and contain only letters, numbers, and underscores. Found: ${name}`);\n }\n const toolDefinition = extractToolDefinition(tool);\n return `{ name: \"${name}\", description: \"${description}\", ${toolDefinition} }`;\n})\n .join(', ');\nconst extractToolDefinition = (tool) => {\n if ('model' in tool) {\n if (!isRef(tool.model))\n throw new Error(`Unexpected model was found in tool ${tool}.`);\n const { model, modelOperation } = tool;\n return `modelName: \"${model.data.link}\", modelOperation: ${modelOperation}`;\n }\n if (!isRef(tool.query))\n throw new Error(`Unexpected query was found in tool ${tool}.`);\n return `queryName: \"${tool.query.data.link}\"`;\n};\n"],"names":[],"mappings":";;;AAEY,MAAC,uBAAuB,GAAG,CAAC,OAAO,EAAE,QAAQ,KAAK;AAC9D,IAAI,MAAM,EAAE,OAAO,EAAE,YAAY,EAAE,sBAAsB,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,OAAO;AACrF,IAAI,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,oBAAoB,CAAC,OAAO,EAAE,QAAQ,CAAC;AAC1E,IAAI,MAAM,IAAI,GAAG;AACjB,QAAQ,OAAO,EAAE,OAAO,CAAC,YAAY;AACrC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,QAAQ,YAAY,EAAE,YAAY,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC;AAC3D,KAAK;AACL;AACA,IAAI,MAAM,SAAS,GAAG,EAAE;AACxB,IAAI,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE;AACrD,QAAQ,SAAS,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;AAC5C,IAAI;AACJ;AACA,IAAI,MAAM,wBAAwB,GAAG,qBAAqB,CAAC,sBAAsB,CAAC;AAClF,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