Skip to content
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

fix(ai): fix renders for empty toolUse messages #5799

Merged
merged 4 commits into from
Sep 16, 2024
Merged

Conversation

thaddmt
Copy link
Member

@thaddmt thaddmt commented Sep 13, 2024

Description of changes

  • Developer can override the sendMessage utility to send messages with tools that are not renderable in the AIConversation component example below:
  • Updates all of our response components to be prefixed with UI_ to be easier to tell as response component tool uses rather than regular tool uses
  • Filters out messages that are not renderable, i.e only render empty tool use blocks
handleSendMessage={(content) =>
          sendMessage({
            ...content,
            toolConfiguration: {
              tools: {
                generateRecipe: {
                  description: "List ingredients needed for a recipe",
                  inputSchema: {
                    json: {
                      type: "object",
                      properties: {
                        ingredients: {
                          type: "array",
                          items: {
                            type: "object",
                            properties: {
                              ingredientName: { type: "string" },
                              quantity: { type: "number" },
                              unit: { type: "string" },
                            }
                          },
                        },
                      },
                    },
                  },
                },
              }
            }
          })
        }

Issue #, if available

Description of how you validated changes

Checklist

  • Have read the Pull Request Guidelines
  • PR description included
  • yarn test passes and tests are updated/added
  • PR title and commit messages follow conventional commit syntax
  • If this change should result in a version bump, changeset added (This can be done after creating the PR.) This does not apply to changes made to docs, e2e, examples, or other private packages.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@thaddmt thaddmt requested a review from a team as a code owner September 13, 2024 17:18
Copy link

changeset-bot bot commented Sep 13, 2024

🦋 Changeset detected

Latest commit: cbd0f70

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/ui-react-ai Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

export const ResponseComponentsContext =
React.createContext<ResponseComponentsContextProps>(undefined);

const prependResponseComponents = (responseComponents?: ResponseComponents) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it so that we have to run this utility every time responseComponents changes, which responseComponents is set on the createAIConversation so it shouldnt be changing much if at all

dbanksdesign
dbanksdesign previously approved these changes Sep 13, 2024
@@ -4,9 +4,22 @@ import { ToolConfiguration } from '../../../types';

type ResponseComponentsContextProps = ResponseComponents | undefined;

export const RESPONSE_COMPONENT_PREFIX = 'UI_';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably overkill and something i usually nitpick, but should we make this more specific by prefixing AMPLIFY_ as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, happy to add

@@ -15,7 +28,9 @@ export const ResponseComponentsProvider = ({
responseComponents?: ResponseComponents;
}): JSX.Element => {
return (
<ResponseComponentsContext.Provider value={responseComponents}>
<ResponseComponentsContext.Provider
value={prependResponseComponents(responseComponents)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will create a new reference on each render, regardless of whether responseComments changes. Tbh kind of surprised that the React linting rules aren't complaining

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I guess I'm used to our linting rules complaining abou this if you send in an object like {foo, bar} so when this didn't complain i was a little unsure lol

@thaddmt thaddmt merged commit b80bab0 into main Sep 16, 2024
34 checks passed
@thaddmt thaddmt deleted the ai-fix-tooluse-renders branch September 16, 2024 21:24
@github-actions github-actions bot mentioned this pull request Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants