@aws-amplify/data-schema 1.4.1 → 1.5.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.
Files changed (174) hide show
  1. package/dist/cjs/ClientSchema/ai/ClientConversation.js +6 -0
  2. package/dist/cjs/ClientSchema/ai/ClientConversation.js.map +1 -0
  3. package/dist/cjs/CustomOperation.js +22 -2
  4. package/dist/cjs/CustomOperation.js.map +1 -1
  5. package/dist/cjs/SchemaProcessor.js +41 -5
  6. package/dist/cjs/SchemaProcessor.js.map +1 -1
  7. package/dist/cjs/a.js +9 -1
  8. package/dist/cjs/a.js.map +1 -1
  9. package/dist/cjs/ai/ConversationSchemaTypes.js +210 -0
  10. package/dist/cjs/ai/ConversationSchemaTypes.js.map +1 -0
  11. package/dist/cjs/ai/ConversationType.js +27 -0
  12. package/dist/cjs/ai/ConversationType.js.map +1 -0
  13. package/dist/cjs/ai/ModelType.js +37 -0
  14. package/dist/cjs/ai/ModelType.js.map +1 -0
  15. package/dist/cjs/ai/types/ConversationMessageContent.js +6 -0
  16. package/dist/cjs/ai/types/ConversationMessageContent.js.map +1 -0
  17. package/dist/cjs/ai/types/ToolConfiguration.js +6 -0
  18. package/dist/cjs/ai/types/ToolConfiguration.js.map +1 -0
  19. package/dist/cjs/ai/types/ToolResultContent.js +6 -0
  20. package/dist/cjs/ai/types/ToolResultContent.js.map +1 -0
  21. package/dist/cjs/ai/types/contentBlocks.js +6 -0
  22. package/dist/cjs/ai/types/contentBlocks.js.map +1 -0
  23. package/dist/cjs/runtime/addSchemaToClient.js +2 -0
  24. package/dist/cjs/runtime/addSchemaToClient.js.map +1 -1
  25. package/dist/cjs/runtime/internals/APIClient.js +3 -3
  26. package/dist/cjs/runtime/internals/APIClient.js.map +1 -1
  27. package/dist/cjs/runtime/internals/ai/conversationMessageDeserializers.js +54 -0
  28. package/dist/cjs/runtime/internals/ai/conversationMessageDeserializers.js.map +1 -0
  29. package/dist/cjs/runtime/internals/ai/conversationMessageSerializers.js +58 -0
  30. package/dist/cjs/runtime/internals/ai/conversationMessageSerializers.js.map +1 -0
  31. package/dist/cjs/runtime/internals/ai/convertItemToConversation.js +22 -0
  32. package/dist/cjs/runtime/internals/ai/convertItemToConversation.js.map +1 -0
  33. package/dist/cjs/runtime/internals/ai/convertItemToConversationMessage.js +16 -0
  34. package/dist/cjs/runtime/internals/ai/convertItemToConversationMessage.js.map +1 -0
  35. package/dist/cjs/runtime/internals/ai/createCreateConversationFunction.js +18 -0
  36. package/dist/cjs/runtime/internals/ai/createCreateConversationFunction.js.map +1 -0
  37. package/dist/cjs/runtime/internals/ai/createGetConversationFunction.js +20 -0
  38. package/dist/cjs/runtime/internals/ai/createGetConversationFunction.js.map +1 -0
  39. package/dist/cjs/runtime/internals/ai/createListConversationsFunction.js +21 -0
  40. package/dist/cjs/runtime/internals/ai/createListConversationsFunction.js.map +1 -0
  41. package/dist/cjs/runtime/internals/ai/createListMessagesFunction.js +22 -0
  42. package/dist/cjs/runtime/internals/ai/createListMessagesFunction.js.map +1 -0
  43. package/dist/cjs/runtime/internals/ai/createOnMessageFunction.js +22 -0
  44. package/dist/cjs/runtime/internals/ai/createOnMessageFunction.js.map +1 -0
  45. package/dist/cjs/runtime/internals/ai/createSendMessageFunction.js +32 -0
  46. package/dist/cjs/runtime/internals/ai/createSendMessageFunction.js.map +1 -0
  47. package/dist/cjs/runtime/internals/index.js +5 -1
  48. package/dist/cjs/runtime/internals/index.js.map +1 -1
  49. package/dist/cjs/runtime/internals/operations/custom.js +17 -10
  50. package/dist/cjs/runtime/internals/operations/custom.js.map +1 -1
  51. package/dist/cjs/runtime/internals/utils/clientProperties/generateConversationsProperty.js +48 -0
  52. package/dist/cjs/runtime/internals/utils/clientProperties/generateConversationsProperty.js.map +1 -0
  53. package/dist/cjs/runtime/internals/utils/clientProperties/generateGenerationsProperty.js +22 -0
  54. package/dist/cjs/runtime/internals/utils/clientProperties/generateGenerationsProperty.js.map +1 -0
  55. package/dist/esm/ClientSchema/Core/ClientSchemaProperty.d.ts +1 -1
  56. package/dist/esm/ClientSchema/ai/ClientConversation.d.ts +7 -0
  57. package/dist/esm/ClientSchema/ai/ClientConversation.mjs +2 -0
  58. package/dist/esm/ClientSchema/ai/ClientConversation.mjs.map +1 -0
  59. package/dist/esm/ClientSchema/index.d.ts +12 -2
  60. package/dist/esm/CustomOperation.d.ts +33 -2
  61. package/dist/esm/CustomOperation.mjs +21 -2
  62. package/dist/esm/CustomOperation.mjs.map +1 -1
  63. package/dist/esm/ModelSchema.d.ts +3 -1
  64. package/dist/esm/SchemaProcessor.d.ts +1 -1
  65. package/dist/esm/SchemaProcessor.mjs +41 -5
  66. package/dist/esm/SchemaProcessor.mjs.map +1 -1
  67. package/dist/esm/a.d.ts +7 -2
  68. package/dist/esm/a.mjs +9 -1
  69. package/dist/esm/a.mjs.map +1 -1
  70. package/dist/esm/ai/ConversationSchemaTypes.d.ts +3 -0
  71. package/dist/esm/ai/ConversationSchemaTypes.mjs +208 -0
  72. package/dist/esm/ai/ConversationSchemaTypes.mjs.map +1 -0
  73. package/dist/esm/ai/ConversationType.d.ts +102 -0
  74. package/dist/esm/ai/ConversationType.mjs +25 -0
  75. package/dist/esm/ai/ConversationType.mjs.map +1 -0
  76. package/dist/esm/ai/ModelType.d.ts +30 -0
  77. package/dist/esm/ai/ModelType.mjs +34 -0
  78. package/dist/esm/ai/ModelType.mjs.map +1 -0
  79. package/dist/esm/ai/types/ConversationMessageContent.d.ts +27 -0
  80. package/dist/esm/ai/types/ConversationMessageContent.mjs +2 -0
  81. package/dist/esm/ai/types/ConversationMessageContent.mjs.map +1 -0
  82. package/dist/esm/ai/types/ToolConfiguration.d.ts +18 -0
  83. package/dist/esm/ai/types/ToolConfiguration.mjs +2 -0
  84. package/dist/esm/ai/types/ToolConfiguration.mjs.map +1 -0
  85. package/dist/esm/ai/types/ToolResultContent.d.ts +18 -0
  86. package/dist/esm/ai/types/ToolResultContent.mjs +2 -0
  87. package/dist/esm/ai/types/ToolResultContent.mjs.map +1 -0
  88. package/dist/esm/ai/types/contentBlocks.d.ts +19 -0
  89. package/dist/esm/ai/types/contentBlocks.mjs +2 -0
  90. package/dist/esm/ai/types/contentBlocks.mjs.map +1 -0
  91. package/dist/esm/runtime/addSchemaToClient.mjs +4 -0
  92. package/dist/esm/runtime/addSchemaToClient.mjs.map +1 -1
  93. package/dist/esm/runtime/addSchemaToClientWithInstance.mjs +2 -1
  94. package/dist/esm/runtime/addSchemaToClientWithInstance.mjs.map +1 -1
  95. package/dist/esm/runtime/bridge-types.d.ts +29 -3
  96. package/dist/esm/runtime/client/index.d.ts +8 -1
  97. package/dist/esm/runtime/internals/APIClient.mjs +3 -3
  98. package/dist/esm/runtime/internals/APIClient.mjs.map +1 -1
  99. package/dist/esm/runtime/internals/ai/conversationMessageDeserializers.d.ts +2 -0
  100. package/dist/esm/runtime/internals/ai/conversationMessageDeserializers.mjs +52 -0
  101. package/dist/esm/runtime/internals/ai/conversationMessageDeserializers.mjs.map +1 -0
  102. package/dist/esm/runtime/internals/ai/conversationMessageSerializers.d.ts +37 -0
  103. package/dist/esm/runtime/internals/ai/conversationMessageSerializers.mjs +54 -0
  104. package/dist/esm/runtime/internals/ai/conversationMessageSerializers.mjs.map +1 -0
  105. package/dist/esm/runtime/internals/ai/convertItemToConversation.d.ts +3 -0
  106. package/dist/esm/runtime/internals/ai/convertItemToConversation.mjs +20 -0
  107. package/dist/esm/runtime/internals/ai/convertItemToConversation.mjs.map +1 -0
  108. package/dist/esm/runtime/internals/ai/convertItemToConversationMessage.d.ts +7 -0
  109. package/dist/esm/runtime/internals/ai/convertItemToConversationMessage.mjs +14 -0
  110. package/dist/esm/runtime/internals/ai/convertItemToConversationMessage.mjs.map +1 -0
  111. package/dist/esm/runtime/internals/ai/createCreateConversationFunction.d.ts +3 -0
  112. package/dist/esm/runtime/internals/ai/createCreateConversationFunction.mjs +16 -0
  113. package/dist/esm/runtime/internals/ai/createCreateConversationFunction.mjs.map +1 -0
  114. package/dist/esm/runtime/internals/ai/createGetConversationFunction.d.ts +3 -0
  115. package/dist/esm/runtime/internals/ai/createGetConversationFunction.mjs +18 -0
  116. package/dist/esm/runtime/internals/ai/createGetConversationFunction.mjs.map +1 -0
  117. package/dist/esm/runtime/internals/ai/createListConversationsFunction.d.ts +3 -0
  118. package/dist/esm/runtime/internals/ai/createListConversationsFunction.mjs +19 -0
  119. package/dist/esm/runtime/internals/ai/createListConversationsFunction.mjs.map +1 -0
  120. package/dist/esm/runtime/internals/ai/createListMessagesFunction.d.ts +3 -0
  121. package/dist/esm/runtime/internals/ai/createListMessagesFunction.mjs +20 -0
  122. package/dist/esm/runtime/internals/ai/createListMessagesFunction.mjs.map +1 -0
  123. package/dist/esm/runtime/internals/ai/createOnMessageFunction.d.ts +3 -0
  124. package/dist/esm/runtime/internals/ai/createOnMessageFunction.mjs +20 -0
  125. package/dist/esm/runtime/internals/ai/createOnMessageFunction.mjs.map +1 -0
  126. package/dist/esm/runtime/internals/ai/createSendMessageFunction.d.ts +3 -0
  127. package/dist/esm/runtime/internals/ai/createSendMessageFunction.mjs +30 -0
  128. package/dist/esm/runtime/internals/ai/createSendMessageFunction.mjs.map +1 -0
  129. package/dist/esm/runtime/internals/index.d.ts +2 -0
  130. package/dist/esm/runtime/internals/index.mjs +2 -0
  131. package/dist/esm/runtime/internals/index.mjs.map +1 -1
  132. package/dist/esm/runtime/internals/operations/custom.d.ts +1 -1
  133. package/dist/esm/runtime/internals/operations/custom.mjs +17 -10
  134. package/dist/esm/runtime/internals/operations/custom.mjs.map +1 -1
  135. package/dist/esm/runtime/internals/utils/clientProperties/generateConversationsProperty.d.ts +3 -0
  136. package/dist/esm/runtime/internals/utils/clientProperties/generateConversationsProperty.mjs +46 -0
  137. package/dist/esm/runtime/internals/utils/clientProperties/generateConversationsProperty.mjs.map +1 -0
  138. package/dist/esm/runtime/internals/utils/clientProperties/generateGenerationsProperty.d.ts +3 -0
  139. package/dist/esm/runtime/internals/utils/clientProperties/generateGenerationsProperty.mjs +20 -0
  140. package/dist/esm/runtime/internals/utils/clientProperties/generateGenerationsProperty.mjs.map +1 -0
  141. package/dist/meta/cjs.tsbuildinfo +1 -1
  142. package/package.json +3 -1
  143. package/src/ClientSchema/Core/ClientSchemaProperty.ts +3 -1
  144. package/src/ClientSchema/ai/ClientConversation.ts +15 -0
  145. package/src/ClientSchema/index.ts +15 -1
  146. package/src/CustomOperation.ts +48 -5
  147. package/src/ModelSchema.ts +4 -1
  148. package/src/SchemaProcessor.ts +73 -8
  149. package/src/a.ts +10 -1
  150. package/src/ai/ConversationSchemaTypes.ts +253 -0
  151. package/src/ai/ConversationType.ts +135 -0
  152. package/src/ai/ModelType.ts +43 -0
  153. package/src/ai/types/ConversationMessageContent.ts +43 -0
  154. package/src/ai/types/ToolConfiguration.ts +24 -0
  155. package/src/ai/types/ToolResultContent.ts +28 -0
  156. package/src/ai/types/contentBlocks.ts +26 -0
  157. package/src/runtime/addSchemaToClient.ts +12 -0
  158. package/src/runtime/bridge-types.ts +41 -4
  159. package/src/runtime/client/index.ts +17 -1
  160. package/src/runtime/internals/APIClient.ts +3 -4
  161. package/src/runtime/internals/ai/conversationMessageDeserializers.ts +62 -0
  162. package/src/runtime/internals/ai/conversationMessageSerializers.ts +68 -0
  163. package/src/runtime/internals/ai/convertItemToConversation.ts +53 -0
  164. package/src/runtime/internals/ai/convertItemToConversationMessage.ts +18 -0
  165. package/src/runtime/internals/ai/createCreateConversationFunction.ts +44 -0
  166. package/src/runtime/internals/ai/createGetConversationFunction.ts +48 -0
  167. package/src/runtime/internals/ai/createListConversationsFunction.ts +46 -0
  168. package/src/runtime/internals/ai/createListMessagesFunction.ts +42 -0
  169. package/src/runtime/internals/ai/createOnMessageFunction.ts +41 -0
  170. package/src/runtime/internals/ai/createSendMessageFunction.ts +60 -0
  171. package/src/runtime/internals/index.ts +2 -0
  172. package/src/runtime/internals/operations/custom.ts +24 -12
  173. package/src/runtime/internals/utils/clientProperties/generateConversationsProperty.ts +91 -0
  174. package/src/runtime/internals/utils/clientProperties/generateGenerationsProperty.ts +41 -0
@@ -248,7 +248,7 @@ export function initializeModel(
248
248
  );
249
249
 
250
250
  // if get is disabled on the related model
251
- if ((client as any).models[relatedModelName].get === undefined) {
251
+ if ((client as any).models[relatedModelName]?.get === undefined) {
252
252
  break;
253
253
  }
254
254
 
@@ -340,7 +340,7 @@ export function initializeModel(
340
340
  );
341
341
 
342
342
  // if list is disabled on the related model
343
- if ((client as any).models[relatedModelName].list === undefined) {
343
+ if ((client as any).models[relatedModelName]?.list === undefined) {
344
344
  break;
345
345
  }
346
346
 
@@ -411,7 +411,7 @@ export function initializeModel(
411
411
  );
412
412
 
413
413
  // if list is disabled on the related model
414
- if ((client as any).models[relatedModelName].list === undefined) {
414
+ if ((client as any).models[relatedModelName]?.list === undefined) {
415
415
  break;
416
416
  }
417
417
 
@@ -1242,7 +1242,6 @@ export function buildGraphQLVariables(
1242
1242
  throw new Error(
1243
1243
  'Internal error: Attempted to build variables for observeQuery. Please report this error.',
1244
1244
  );
1245
- break;
1246
1245
  default: {
1247
1246
  const exhaustiveCheck: never = operation;
1248
1247
  throw new Error(`Unhandled operation case: ${exhaustiveCheck}`);
@@ -0,0 +1,62 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { fromBase64 } from '@smithy/util-base64';
5
+ import type { ConversationMessageContent } from '../../../ai/types/ConversationMessageContent';
6
+
7
+ export const deserializeContent = (
8
+ content: Record<string, any>[],
9
+ ): ConversationMessageContent[] =>
10
+ content.map((block) => {
11
+ if (block.image) {
12
+ return deserializeImageBlock(block);
13
+ }
14
+ if (block.toolUse) {
15
+ return deserializeToolUseBlock(block);
16
+ }
17
+ if (block.toolResult) {
18
+ return deserializeToolResultBlock(block);
19
+ }
20
+ return removeNullsFromBlock(block) as ConversationMessageContent;
21
+ });
22
+
23
+ const deserializeImageBlock = ({ image }: Record<'image', any>) => ({
24
+ image: {
25
+ ...image,
26
+ source: {
27
+ ...image.source,
28
+ bytes: fromBase64(image.source.bytes),
29
+ },
30
+ },
31
+ });
32
+
33
+ const deserializeJsonBlock = ({ json }: Record<'json', any>) => ({
34
+ json: JSON.parse(json),
35
+ });
36
+
37
+ const deserializeToolUseBlock = ({ toolUse }: Record<'toolUse', any>) => ({
38
+ toolUse: {
39
+ ...toolUse,
40
+ input: JSON.parse(toolUse.input),
41
+ },
42
+ });
43
+
44
+ const deserializeToolResultBlock = ({
45
+ toolResult,
46
+ }: Record<'toolResult', any>) => ({
47
+ toolResult: {
48
+ toolUseId: toolResult.toolUseId,
49
+ content: toolResult.content.map((toolResultBlock: Record<string, any>) => {
50
+ if (toolResultBlock.image) {
51
+ return deserializeImageBlock(toolResultBlock);
52
+ }
53
+ if (toolResultBlock.json) {
54
+ return deserializeJsonBlock(toolResultBlock);
55
+ }
56
+ return removeNullsFromBlock(toolResultBlock);
57
+ }),
58
+ },
59
+ });
60
+
61
+ const removeNullsFromBlock = (block: Record<string, any>) =>
62
+ Object.fromEntries(Object.entries(block).filter(([_, v]) => v !== null));
@@ -0,0 +1,68 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { toBase64 } from '@smithy/util-base64';
5
+ import type {
6
+ ConversationMessageContent,
7
+ ConversationMessageImageContent,
8
+ ConversationMessageToolResultContent,
9
+ } from '../../../ai/types/ConversationMessageContent';
10
+ import type { ToolConfiguration } from '../../../ai/types/ToolConfiguration';
11
+ import type { ToolResultJsonContent } from '../../../ai/types/ToolResultContent';
12
+
13
+ export const serializeAiContext = (aiContext: string | Record<string, any>) =>
14
+ JSON.stringify(aiContext);
15
+
16
+ export const serializeContent = (content: ConversationMessageContent[]) =>
17
+ content.map((block) => {
18
+ if (block.image) {
19
+ return serializeImageBlock(block);
20
+ }
21
+ if (block.toolResult) {
22
+ return serializeToolResultBlock(block);
23
+ }
24
+ return block;
25
+ });
26
+
27
+ export const serializeToolConfiguration = ({ tools }: ToolConfiguration) => ({
28
+ tools: Object.entries(tools).map(([name, tool]) => ({
29
+ toolSpec: {
30
+ name,
31
+ description: tool.description,
32
+ inputSchema: {
33
+ json: JSON.stringify(tool.inputSchema.json),
34
+ },
35
+ },
36
+ })),
37
+ });
38
+
39
+ const serializeImageBlock = ({ image }: ConversationMessageImageContent) => ({
40
+ image: {
41
+ ...image,
42
+ source: {
43
+ ...image.source,
44
+ bytes: toBase64(image.source.bytes),
45
+ },
46
+ },
47
+ });
48
+
49
+ const serializeJsonBlock = ({ json }: ToolResultJsonContent) => ({
50
+ json: JSON.stringify(json),
51
+ });
52
+
53
+ const serializeToolResultBlock = ({
54
+ toolResult,
55
+ }: ConversationMessageToolResultContent) => ({
56
+ toolResult: {
57
+ ...toolResult,
58
+ content: toolResult.content.map((toolResultBlock) => {
59
+ if (toolResultBlock.image) {
60
+ return serializeImageBlock(toolResultBlock);
61
+ }
62
+ if (toolResultBlock.json) {
63
+ return serializeJsonBlock(toolResultBlock);
64
+ }
65
+ return toolResultBlock;
66
+ }),
67
+ },
68
+ });
@@ -0,0 +1,53 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Conversation } from '../../../ai/ConversationType';
5
+ import type {
6
+ BaseBrowserClient,
7
+ BaseClient,
8
+ ClientInternalsGetter,
9
+ ModelIntrospectionSchema,
10
+ SchemaModel,
11
+ } from '../../bridge-types';
12
+ import { createListMessagesFunction } from './createListMessagesFunction';
13
+ import { createOnMessageFunction } from './createOnMessageFunction';
14
+ import { createSendMessageFunction } from './createSendMessageFunction';
15
+
16
+ export const convertItemToConversation = (
17
+ client: BaseClient,
18
+ modelIntrospection: ModelIntrospectionSchema,
19
+ conversationId: string,
20
+ conversationRouteName: string,
21
+ conversationMessageModel: SchemaModel,
22
+ getInternals: ClientInternalsGetter,
23
+ ): Conversation => {
24
+ if (!conversationId) {
25
+ throw new Error(
26
+ `An error occurred converting a ${conversationRouteName} conversation: Missing ID`,
27
+ );
28
+ }
29
+ return {
30
+ id: conversationId,
31
+ onMessage: createOnMessageFunction(
32
+ client as BaseBrowserClient,
33
+ modelIntrospection,
34
+ conversationId,
35
+ conversationRouteName,
36
+ getInternals,
37
+ ),
38
+ sendMessage: createSendMessageFunction(
39
+ client,
40
+ modelIntrospection,
41
+ conversationId,
42
+ conversationRouteName,
43
+ getInternals,
44
+ ),
45
+ listMessages: createListMessagesFunction(
46
+ client,
47
+ modelIntrospection,
48
+ conversationId,
49
+ conversationMessageModel,
50
+ getInternals,
51
+ ),
52
+ };
53
+ };
@@ -0,0 +1,18 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { deserializeContent } from './conversationMessageDeserializers';
5
+
6
+ export const convertItemToConversationMessage = ({
7
+ content,
8
+ createdAt,
9
+ id,
10
+ conversationId,
11
+ role,
12
+ }: any) => ({
13
+ content: deserializeContent(content),
14
+ conversationId,
15
+ createdAt,
16
+ id,
17
+ role,
18
+ });
@@ -0,0 +1,44 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { ConversationRoute } from '../../../ai/ConversationType';
5
+ import type { SingularReturnValue } from '../../../runtime/client';
6
+ import type {
7
+ BaseClient,
8
+ ClientInternalsGetter,
9
+ ModelIntrospectionSchema,
10
+ SchemaModel,
11
+ } from '../../bridge-types';
12
+ import { getFactory } from '../operations/get';
13
+ import { convertItemToConversation } from './convertItemToConversation';
14
+
15
+ export const createCreateConversationFunction =
16
+ (
17
+ client: BaseClient,
18
+ modelIntrospection: ModelIntrospectionSchema,
19
+ conversationRouteName: string,
20
+ conversationModel: SchemaModel,
21
+ conversationMessageModel: SchemaModel,
22
+ getInternals: ClientInternalsGetter,
23
+ ): ConversationRoute['create'] =>
24
+ async () => {
25
+ const get = getFactory(
26
+ client,
27
+ modelIntrospection,
28
+ conversationModel,
29
+ 'CREATE',
30
+ getInternals,
31
+ ) as () => SingularReturnValue<Record<string, any>>;
32
+ const { data, errors } = await get();
33
+ return {
34
+ data: convertItemToConversation(
35
+ client,
36
+ modelIntrospection,
37
+ data?.id,
38
+ conversationRouteName,
39
+ conversationMessageModel,
40
+ getInternals,
41
+ ),
42
+ errors,
43
+ };
44
+ };
@@ -0,0 +1,48 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { ConversationRoute } from '../../../ai/ConversationType';
5
+ import type { SingularReturnValue } from '../../../runtime/client';
6
+ import type {
7
+ BaseClient,
8
+ ClientInternalsGetter,
9
+ ModelIntrospectionSchema,
10
+ SchemaModel,
11
+ } from '../../bridge-types';
12
+ import { getFactory } from '../operations/get';
13
+ import { convertItemToConversation } from './convertItemToConversation';
14
+
15
+ export const createGetConversationFunction =
16
+ (
17
+ client: BaseClient,
18
+ modelIntrospection: ModelIntrospectionSchema,
19
+ conversationRouteName: string,
20
+ conversationModel: SchemaModel,
21
+ conversationMessageModel: SchemaModel,
22
+ getInternals: ClientInternalsGetter,
23
+ ): ConversationRoute['get'] =>
24
+ async ({ id }) => {
25
+ const get = getFactory(
26
+ client,
27
+ modelIntrospection,
28
+ conversationModel,
29
+ 'GET',
30
+ getInternals,
31
+ ) as (
32
+ args?: Record<string, any>,
33
+ ) => SingularReturnValue<Record<string, any>>;
34
+ const { data, errors } = await get({ id });
35
+ return {
36
+ data: data
37
+ ? convertItemToConversation(
38
+ client,
39
+ modelIntrospection,
40
+ data.id,
41
+ conversationRouteName,
42
+ conversationMessageModel,
43
+ getInternals,
44
+ )
45
+ : data,
46
+ errors,
47
+ };
48
+ };
@@ -0,0 +1,46 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { ConversationRoute } from '../../../ai/ConversationType';
5
+ import type { ListReturnValue } from '../../../runtime/client';
6
+ import type {
7
+ BaseClient,
8
+ ClientInternalsGetter,
9
+ ModelIntrospectionSchema,
10
+ SchemaModel,
11
+ } from '../../bridge-types';
12
+ import { listFactory } from '../operations/list';
13
+ import { convertItemToConversation } from './convertItemToConversation';
14
+
15
+ export const createListConversationsFunction =
16
+ (
17
+ client: BaseClient,
18
+ modelIntrospection: ModelIntrospectionSchema,
19
+ conversationRouteName: string,
20
+ conversationModel: SchemaModel,
21
+ conversationMessageModel: SchemaModel,
22
+ getInternals: ClientInternalsGetter,
23
+ ): ConversationRoute['list'] =>
24
+ async (input) => {
25
+ const list = listFactory(
26
+ client,
27
+ modelIntrospection,
28
+ conversationModel,
29
+ getInternals,
30
+ ) as (args?: Record<string, any>) => ListReturnValue<Record<string, any>>;
31
+ const { data, nextToken, errors } = await list(input);
32
+ return {
33
+ data: data.map((datum: Record<string, any>) => {
34
+ return convertItemToConversation(
35
+ client,
36
+ modelIntrospection,
37
+ datum.id,
38
+ conversationRouteName,
39
+ conversationMessageModel,
40
+ getInternals,
41
+ );
42
+ }),
43
+ nextToken,
44
+ errors,
45
+ };
46
+ };
@@ -0,0 +1,42 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ Conversation,
6
+ ConversationMessage,
7
+ } from '../../../ai/ConversationType';
8
+ import type { ListReturnValue } from '../../../runtime/client';
9
+ import type {
10
+ BaseClient,
11
+ ClientInternalsGetter,
12
+ ModelIntrospectionSchema,
13
+ SchemaModel,
14
+ } from '../../bridge-types';
15
+ import { listFactory } from '../operations/list';
16
+ import { convertItemToConversationMessage } from './convertItemToConversationMessage';
17
+
18
+ export const createListMessagesFunction =
19
+ (
20
+ client: BaseClient,
21
+ modelIntrospection: ModelIntrospectionSchema,
22
+ conversationId: string,
23
+ conversationMessageModel: SchemaModel,
24
+ getInternals: ClientInternalsGetter,
25
+ ): Conversation['listMessages'] =>
26
+ async (input) => {
27
+ const list = listFactory(
28
+ client,
29
+ modelIntrospection,
30
+ conversationMessageModel,
31
+ getInternals,
32
+ ) as (args?: Record<string, any>) => ListReturnValue<ConversationMessage>;
33
+ const { data, nextToken, errors } = await list({
34
+ ...input,
35
+ filter: { conversationId: { eq: conversationId } },
36
+ });
37
+ return {
38
+ data: data.map((item: any) => convertItemToConversationMessage(item)),
39
+ nextToken,
40
+ errors,
41
+ };
42
+ };
@@ -0,0 +1,41 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Observable, Subscription } from 'rxjs';
5
+ import type { Conversation } from '../../../ai/ConversationType';
6
+ import type {
7
+ BaseClient,
8
+ ClientInternalsGetter,
9
+ ModelIntrospectionSchema,
10
+ } from '../../bridge-types';
11
+ import { customOpFactory } from '../operations/custom';
12
+ import { convertItemToConversationMessage } from './convertItemToConversationMessage';
13
+
14
+ export const createOnMessageFunction =
15
+ (
16
+ client: BaseClient,
17
+ modelIntrospection: ModelIntrospectionSchema,
18
+ conversationId: string,
19
+ conversationRouteName: string,
20
+ getInternals: ClientInternalsGetter,
21
+ ): Conversation['onMessage'] =>
22
+ (handler): Subscription => {
23
+ const { conversations } = modelIntrospection;
24
+ // Safe guard for standalone function. When called as part of client generation, this should never be falsy.
25
+ if (!conversations) {
26
+ return {} as Subscription;
27
+ }
28
+ const subscribeSchema =
29
+ conversations[conversationRouteName].message.subscribe;
30
+ const subscribeOperation = customOpFactory(
31
+ client,
32
+ modelIntrospection,
33
+ 'subscription',
34
+ subscribeSchema,
35
+ false,
36
+ getInternals,
37
+ ) as (args?: Record<string, any>) => Observable<any>;
38
+ return subscribeOperation({ conversationId }).subscribe((data) => {
39
+ handler(convertItemToConversationMessage(data));
40
+ });
41
+ };
@@ -0,0 +1,60 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { SingularReturnValue } from '../..';
5
+ import type {
6
+ Conversation,
7
+ ConversationMessage,
8
+ } from '../../../ai/ConversationType';
9
+ import type {
10
+ BaseClient,
11
+ ClientInternalsGetter,
12
+ ModelIntrospectionSchema,
13
+ } from '../../bridge-types';
14
+ import { customOpFactory } from '../operations/custom';
15
+ import { convertItemToConversationMessage } from './convertItemToConversationMessage';
16
+ import {
17
+ serializeAiContext,
18
+ serializeContent,
19
+ serializeToolConfiguration,
20
+ } from './conversationMessageSerializers';
21
+
22
+ export const createSendMessageFunction =
23
+ (
24
+ client: BaseClient,
25
+ modelIntrospection: ModelIntrospectionSchema,
26
+ conversationId: string,
27
+ conversationRouteName: string,
28
+ getInternals: ClientInternalsGetter,
29
+ ): Conversation['sendMessage'] =>
30
+ async ({ aiContext, content, toolConfiguration }) => {
31
+ const { conversations } = modelIntrospection;
32
+
33
+ // Safe guard for standalone function. When called as part of client generation, this should never be falsy.
34
+ if (!conversations) {
35
+ return {} as SingularReturnValue<ConversationMessage>;
36
+ }
37
+ const sendSchema = conversations[conversationRouteName].message.send;
38
+ const sendOperation = customOpFactory(
39
+ client,
40
+ modelIntrospection,
41
+ 'mutation',
42
+ sendSchema,
43
+ false,
44
+ getInternals,
45
+ ) as (
46
+ args?: Record<string, any>,
47
+ ) => SingularReturnValue<ConversationMessage>;
48
+ const { data, errors } = await sendOperation({
49
+ conversationId,
50
+ content: serializeContent(content),
51
+ ...(aiContext && { aiContext: serializeAiContext(aiContext) }),
52
+ ...(toolConfiguration && {
53
+ toolConfiguration: serializeToolConfiguration(toolConfiguration),
54
+ }),
55
+ });
56
+ return {
57
+ data: data ? convertItemToConversationMessage(data) : data,
58
+ errors,
59
+ };
60
+ };
@@ -5,6 +5,8 @@ export {
5
5
  generateCustomQueriesProperty,
6
6
  generateCustomSubscriptionsProperty,
7
7
  } from './generateCustomOperationsProperty';
8
+ export { generateConversationsProperty } from './utils/clientProperties/generateConversationsProperty';
9
+ export { generateGenerationsProperty } from './utils/clientProperties/generateGenerationsProperty';
8
10
  export { generateEnumsProperty } from './utils/clientProperties/generateEnumsProperty';
9
11
  export { generateModelsProperty } from './utils/clientProperties/generateModelsProperty';
10
12
  export { isGraphQLResponseWithErrors } from './utils/runtimeTypeGuards/isGraphQLResponseWithErrors';
@@ -13,6 +13,10 @@ import {
13
13
  ListArgs,
14
14
  QueryArgs,
15
15
  ModelIntrospectionSchema,
16
+ CustomOperationArgument,
17
+ InputFieldType,
18
+ EnumType,
19
+ InputType,
16
20
  } from '../../bridge-types';
17
21
 
18
22
  import { map } from 'rxjs';
@@ -194,19 +198,27 @@ function hasStringField<Field extends string>(
194
198
  return typeof o[field] === 'string';
195
199
  }
196
200
 
201
+ function isEnumType(type: InputFieldType): type is EnumType {
202
+ return type instanceof Object && 'enum' in type;
203
+ }
204
+
205
+ function isInputType(type: InputFieldType): type is InputType {
206
+ return type instanceof Object && 'input' in type;
207
+ }
208
+
197
209
  /**
198
210
  * @param argDef A single argument definition from a custom operation
199
211
  * @returns A string naming the base type including the `!` if the arg is required.
200
212
  */
201
- function argumentBaseTypeString(
202
- argDef: Exclude<CustomOperation['arguments'], undefined>[number],
203
- ) {
204
- const requiredFlag = argDef.isRequired ? '!' : '';
205
- if (argDef.type instanceof Object && 'enum' in argDef.type) {
206
- return argDef.type.enum + requiredFlag;
207
- } else {
208
- return argDef.type + requiredFlag;
213
+ function argumentBaseTypeString({ type, isRequired }: CustomOperationArgument) {
214
+ const requiredFlag = isRequired ? '!' : '';
215
+ if (isEnumType(type)) {
216
+ return `${type.enum}${requiredFlag}`;
217
+ }
218
+ if (isInputType(type)) {
219
+ return `${type.input}${requiredFlag}`;
209
220
  }
221
+ return `${type}${requiredFlag}`;
210
222
  }
211
223
 
212
224
  /**
@@ -233,10 +245,10 @@ function outerArguments(operation: CustomOperation): string {
233
245
  return '';
234
246
  }
235
247
  const args = Object.entries(operation.arguments)
236
- .map(([k, v]) => {
237
- const baseType = argumentBaseTypeString(v);
238
- const finalType = v.isArray
239
- ? `[${baseType}]${v.isArrayNullable ? '' : '!'}`
248
+ .map(([k, argument]) => {
249
+ const baseType = argumentBaseTypeString(argument);
250
+ const finalType = argument.isArray
251
+ ? `[${baseType}]${argument.isArrayNullable ? '' : '!'}`
240
252
  : baseType;
241
253
 
242
254
  return `$${k}: ${finalType}`;
@@ -0,0 +1,91 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { ConversationRoute } from '../../../../ai/ConversationType';
5
+ import {
6
+ BaseClient,
7
+ ClientInternalsGetter,
8
+ GraphQLProviderConfig,
9
+ ModelIntrospectionSchema,
10
+ } from '../../../bridge-types';
11
+ import { createCreateConversationFunction } from '../../ai/createCreateConversationFunction';
12
+ import { createGetConversationFunction } from '../../ai/createGetConversationFunction';
13
+ import { createListConversationsFunction } from '../../ai/createListConversationsFunction';
14
+
15
+ export function generateConversationsProperty(
16
+ client: BaseClient,
17
+ apiGraphQLConfig: GraphQLProviderConfig['GraphQL'],
18
+ getInternals: ClientInternalsGetter,
19
+ ): Record<string, ConversationRoute> {
20
+ const modelIntrospection: ModelIntrospectionSchema | undefined =
21
+ apiGraphQLConfig?.modelIntrospection;
22
+
23
+ // conversations will be absent from model intro schema if no conversation routes
24
+ // are present on the source schema.
25
+ if (!modelIntrospection?.conversations) {
26
+ return {};
27
+ }
28
+
29
+ const conversations: Record<string, ConversationRoute> = {};
30
+
31
+ for (const {
32
+ name,
33
+ conversation,
34
+ message,
35
+ models,
36
+ nonModels,
37
+ enums,
38
+ } of Object.values(modelIntrospection.conversations)) {
39
+ const conversationModel = models[conversation.modelName];
40
+ const conversationMessageModel = models[message.modelName];
41
+
42
+ if (!conversationModel || !conversationMessageModel) {
43
+ return {};
44
+ }
45
+
46
+ const conversationModelIntrospection: ModelIntrospectionSchema = {
47
+ ...modelIntrospection,
48
+ models: {
49
+ ...modelIntrospection.models,
50
+ ...models,
51
+ },
52
+ nonModels: {
53
+ ...modelIntrospection.nonModels,
54
+ ...nonModels,
55
+ },
56
+ enums: {
57
+ ...modelIntrospection.enums,
58
+ ...enums,
59
+ },
60
+ };
61
+
62
+ conversations[name] = {
63
+ create: createCreateConversationFunction(
64
+ client,
65
+ conversationModelIntrospection,
66
+ name,
67
+ conversationModel,
68
+ conversationMessageModel,
69
+ getInternals,
70
+ ),
71
+ get: createGetConversationFunction(
72
+ client,
73
+ conversationModelIntrospection,
74
+ name,
75
+ conversationModel,
76
+ conversationMessageModel,
77
+ getInternals,
78
+ ),
79
+ list: createListConversationsFunction(
80
+ client,
81
+ conversationModelIntrospection,
82
+ name,
83
+ conversationModel,
84
+ conversationMessageModel,
85
+ getInternals,
86
+ ),
87
+ };
88
+ }
89
+
90
+ return conversations;
91
+ }