@aws-amplify/data-schema 1.8.1 → 1.9.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/data-schema",
3
- "version": "1.8.1",
3
+ "version": "1.9.1",
4
4
  "license": "Apache-2.0",
5
5
  "repository": {
6
6
  "type": "git",
@@ -64,7 +64,7 @@
64
64
  "jest": "^29.7.0",
65
65
  "jest-tsd": "^0.2.2",
66
66
  "rimraf": "^5.0.5",
67
- "rollup": "^4.9.6",
67
+ "rollup": "^4.22.4",
68
68
  "ts-jest": "^29.1.1",
69
69
  "turbo": "^1.10.14",
70
70
  "typescript": "^5.1.6"
package/src/ModelField.ts CHANGED
@@ -8,6 +8,11 @@ import type { brandSymbol } from './util/Brand.js';
8
8
  */
9
9
  export const __auth = Symbol('__auth');
10
10
 
11
+ /**
12
+ * Used by `.default()` to represent a generated field (SQL).
13
+ */
14
+ export const __generated = Symbol('__generated');
15
+
11
16
  const brandName = 'modelField';
12
17
 
13
18
  export enum ModelFieldType {
@@ -44,7 +49,7 @@ type FieldData = {
44
49
  required: boolean;
45
50
  array: boolean;
46
51
  arrayRequired: boolean;
47
- default: undefined | ModelFieldTypeParamOuter;
52
+ default: undefined | symbol | ModelFieldTypeParamOuter;
48
53
  authorization: Authorization<any, any, any>[];
49
54
  };
50
55
 
@@ -110,7 +115,7 @@ export type ModelField<
110
115
  * @param value the default value
111
116
  */
112
117
  default(
113
- value: ModelFieldTypeParamOuter,
118
+ value?: ModelFieldTypeParamOuter,
114
119
  ): ModelField<T, UsedMethod | 'default'>;
115
120
  /**
116
121
  * Configures field-level authorization rules. Pass in an array of authorizations `(allow => allow.____)` to mix and match
@@ -178,7 +183,7 @@ function _field<T extends ModelFieldTypeParamOuter>(fieldType: ModelFieldType) {
178
183
  return this;
179
184
  },
180
185
  default(val) {
181
- data.default = val;
186
+ data.default = typeof val === 'undefined' ? __generated : val;
182
187
  _meta.lastInvokedMethod = 'default';
183
188
 
184
189
  return this;
@@ -5,6 +5,7 @@ import {
5
5
  string,
6
6
  type BaseModelField,
7
7
  ModelFieldType,
8
+ __generated,
8
9
  } from './ModelField';
9
10
  import {
10
11
  ModelRelationshipTypes,
@@ -198,7 +199,9 @@ function scalarFieldToGql(
198
199
  }
199
200
  }
200
201
 
201
- if (_default !== undefined) {
202
+ if (_default === __generated) {
203
+ field += ` @default`;
204
+ } else if (_default !== undefined) {
202
205
  field += ` @default(value: "${_default?.toString()}")`;
203
206
  }
204
207
 
@@ -507,6 +510,16 @@ function customOperationToGql(
507
510
  const { aiModel, systemPrompt, inferenceConfiguration } =
508
511
  typeDef.data.input;
509
512
 
513
+ // This is done to escape newlines in potentially multi-line system prompts
514
+ // e.g.
515
+ // generateStuff: a.generation({
516
+ // aiModel: a.ai.model('Claude 3 Haiku'),
517
+ // systemPrompt: `Generate a haiku
518
+ // make it multiline`,
519
+ // }),
520
+ //
521
+ // It doesn't affect non multi-line string inputs for system prompts
522
+ const escapedSystemPrompt = systemPrompt.replace(/\r?\n/g, '\\n');
510
523
  const inferenceConfigurationEntries = Object.entries(
511
524
  inferenceConfiguration ?? {},
512
525
  );
@@ -516,7 +529,7 @@ function customOperationToGql(
516
529
  .map(([key, value]) => `${key}: ${value}`)
517
530
  .join(', ')} }`
518
531
  : '';
519
- gqlHandlerContent += `@generation(aiModel: "${aiModel.resourcePath}", systemPrompt: "${systemPrompt}"${inferenceConfigurationGql}) `;
532
+ gqlHandlerContent += `@generation(aiModel: "${aiModel.resourcePath}", systemPrompt: "${escapedSystemPrompt}"${inferenceConfigurationGql}) `;
520
533
  }
521
534
 
522
535
  const gqlField = `${callSignature}: ${returnTypeName} ${gqlHandlerContent}${authString}`;
@@ -13,7 +13,16 @@ export const createConversationField = (
13
13
 
14
14
  const args: Record<string, string> = {
15
15
  aiModel: aiModel.resourcePath,
16
- systemPrompt,
16
+ // This is done to escape newlines in potentially multi-line system prompts
17
+ // e.g.
18
+ // realtorChat: a.conversation({
19
+ // aiModel: a.ai.model('Claude 3 Haiku'),
20
+ // systemPrompt: `You are a helpful real estate assistant
21
+ // Respond in the poetic form of haiku.`,
22
+ // }),
23
+ //
24
+ // It doesn't affect non multi-line string inputs for system prompts
25
+ systemPrompt: systemPrompt.replace(/\r?\n/g, '\\n'),
17
26
  };
18
27
 
19
28
  if (handler) {