-
Notifications
You must be signed in to change notification settings - Fork 22
fix: escape double quotes and backslashes in AI directive string arguments #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zelys-DFKH
wants to merge
2
commits into
aws-amplify:main
Choose a base branch
from
Zelys-DFKH:fix/escape-double-quotes-in-conversation-schema
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@aws-amplify/data-schema": patch | ||
| --- | ||
|
|
||
| fix: escape double quotes and backslashes in AI directive string arguments | ||
|
|
||
| `@conversation` and `@generation` directives interpolate user-supplied strings (`systemPrompt`, tool `description`) directly into GraphQL SDL without escaping special characters. Any prompt containing a double quote or backslash produces invalid SDL, breaking schema compilation. This fix escapes all GraphQL special characters before interpolation. |
95 changes: 95 additions & 0 deletions
95
packages/data-schema/__tests__/ai/ConversationSchemaProcessor.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { a } from '../../src/index'; | ||
| import { defineFunctionStub } from '../utils'; | ||
|
|
||
| describe('GraphQL string escaping in AI schema directives', () => { | ||
| describe('@generation', () => { | ||
| test('escapes double quotes in systemPrompt', () => { | ||
| const schema = a.schema({ | ||
| Result: a.customType({ value: a.string() }), | ||
| makeResult: a | ||
| .generation({ | ||
| aiModel: a.ai.model('Claude 3 Haiku'), | ||
| systemPrompt: 'Always say "yes" or "no".', | ||
| }) | ||
| .returns(a.ref('Result')), | ||
| }); | ||
|
|
||
| const { schema: graphql } = schema.transform(); | ||
|
|
||
| expect(graphql).toContain('systemPrompt: "Always say \\"yes\\" or \\"no\\"."'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('@conversation', () => { | ||
| test('escapes double quotes in systemPrompt', () => { | ||
| const schema = a.schema({ | ||
| ChatBot: a.conversation({ | ||
| aiModel: a.ai.model('Claude 3 Haiku'), | ||
| systemPrompt: 'Say "hello" and "goodbye" to users.', | ||
| }).authorization((allow) => allow.owner()), | ||
| }); | ||
|
|
||
| const { schema: graphql } = schema.transform(); | ||
|
|
||
| expect(graphql).toContain('systemPrompt: "Say \\"hello\\" and \\"goodbye\\" to users."'); | ||
| }); | ||
|
|
||
| test('escapes double quotes in tool description', () => { | ||
| const handler = defineFunctionStub({}); | ||
| const schema = a.schema({ | ||
| Profile: a.customType({ value: a.integer() }), | ||
| infoQuery: a | ||
| .query() | ||
| .returns(a.ref('Profile')) | ||
| .authorization((allow) => allow.publicApiKey()) | ||
| .handler(a.handler.function(handler)), | ||
|
|
||
| ChatBot: a.conversation({ | ||
| aiModel: a.ai.model('Claude 3 Haiku'), | ||
| systemPrompt: 'You are helpful.', | ||
| tools: [ | ||
| a.ai.dataTool({ | ||
| query: a.ref('infoQuery'), | ||
| name: 'infoQuery', | ||
| description: 'Fetches "live" profile data.', | ||
| }), | ||
| ], | ||
| }).authorization((allow) => allow.owner()), | ||
| }); | ||
|
|
||
| const { schema: graphql } = schema.transform(); | ||
|
|
||
| expect(graphql).toContain('description: "Fetches \\"live\\" profile data."'); | ||
| }); | ||
|
|
||
| test('escapes backslashes in systemPrompt', () => { | ||
| const schema = a.schema({ | ||
| ChatBot: a.conversation({ | ||
| aiModel: a.ai.model('Claude 3 Haiku'), | ||
| systemPrompt: 'Use path C:\\\\docs for all outputs.', | ||
| }).authorization((allow) => allow.owner()), | ||
| }); | ||
|
|
||
| const { schema: graphql } = schema.transform(); | ||
|
|
||
| expect(graphql).toContain('systemPrompt: "Use path C:\\\\\\\\docs for all outputs."'); | ||
| }); | ||
|
|
||
| test('preserves newline escaping in multiline systemPrompt', () => { | ||
| const schema = a.schema({ | ||
| ChatBot: a.conversation({ | ||
| aiModel: a.ai.model('Claude 3 Haiku'), | ||
| systemPrompt: `You are helpful. | ||
| Respond in haiku.`, | ||
| }).authorization((allow) => allow.owner()), | ||
| }); | ||
|
|
||
| const { schema: graphql } = schema.transform(); | ||
|
|
||
| expect(graphql).toContain('systemPrompt: "You are helpful.\\nRespond in haiku."'); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this follow the same approach?
amplify-data/packages/data-schema/src/SchemaProcessor.ts
Lines 581 to 583 in 2d75518
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. The
@generationpath already usesJSON.stringifyand I should have been consistent here too. I'll swap the manual regex forJSON.stringify(str).slice(1, -1)to use the same core while keeping the call-site quoting pattern. Pushing the fix now.