From d5b0f34e1b83f461169d55a748077c0dcb06c2ee Mon Sep 17 00:00:00 2001 From: retrofox Date: Thu, 27 Jul 2023 17:13:04 +0000 Subject: [PATCH] AI Client: handle properly passing the post_id parameter to endpoint (#32104) * [not verified] propagate better postId from hook to event source * [not verified] update doc * changelog * populate post id by using the askQuestionOptions * [not verified] changelog * [not verified] update pnpm Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/5683319594 --- CHANGELOG.md | 3 +++ src/ask-question/Readme.md | 2 +- src/hooks/use-ai-suggestions/Readme.md | 3 +-- src/hooks/use-ai-suggestions/index.ts | 26 ++++---------------------- src/suggestions-event-source/index.ts | 12 +++++++++++- 5 files changed, 20 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4dd637..bcd9313 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ This is an alpha version! The changes listed here are not final. ### Added - AI Client: Add useAiSuggestions() react custom hook +### Changed +- AI Client: handle properly passing the post_id parameter to endpoint + ## 0.1.0 - 2023-07-25 ### Added - Add Jetpack AI Client [#30855] diff --git a/src/ask-question/Readme.md b/src/ask-question/Readme.md index 664e35a..94495db 100644 --- a/src/ask-question/Readme.md +++ b/src/ask-question/Readme.md @@ -11,7 +11,7 @@ function askQuestion( ): Promise< SuggestionsEventSource > ``` -## Parameters +

Parameters

- `question` (**string** | **PromptItemProps[]**): - The question to ask. diff --git a/src/hooks/use-ai-suggestions/Readme.md b/src/hooks/use-ai-suggestions/Readme.md index f8dc1ec..d9f20ef 100644 --- a/src/hooks/use-ai-suggestions/Readme.md +++ b/src/hooks/use-ai-suggestions/Readme.md @@ -22,8 +22,7 @@ Invokes the custom hook with the provided options. - `prompt: PromptItemProps[]` (optional): An array of request prompts. - `autoRequest: boolean` (optional, defaults to `false`): Determines whether to request suggestions automatically. -- `postId: number`: When defined, will be passed to the askQuestion function. -- `askQuestionOptions: AskQuestionOptionsArgProps` (optional): Options for the askQuestion function. +- `askQuestionOptions: AskQuestionOptionsArgProps` (optional): [Options for the askQuestion](../../ask-question/Readme.md#ask-question-parameters) function. - `onSuggestion: ( suggestion: string ) => void` (optional): A callback function that gets triggered when a suggestion is received. - `onDone: ( content: string ) => void` (optional): A callback function that gets triggered when the process is complete. - `onError: ( error: SuggestionErrorProps ) => void` (optional): A callback function that gets triggered when an error occurs. diff --git a/src/hooks/use-ai-suggestions/index.ts b/src/hooks/use-ai-suggestions/index.ts index 4a569be..11cdaea 100644 --- a/src/hooks/use-ai-suggestions/index.ts +++ b/src/hooks/use-ai-suggestions/index.ts @@ -7,20 +7,20 @@ import debugFactory from 'debug'; /** * Internal dependencies */ -import askQuestion, { AskQuestionOptionsArgProps } from '../../ask-question'; +import askQuestion from '../../ask-question'; import { ERROR_MODERATION, ERROR_NETWORK, ERROR_QUOTA_EXCEEDED, ERROR_SERVICE_UNAVAILABLE, ERROR_UNCLEAR_PROMPT, - type PromptItemProps, - type SuggestionErrorCode, } from '../../types'; /** * Types & constants */ +import type { AskQuestionOptionsArgProps } from '../../ask-question'; import type SuggestionsEventSource from '../../suggestions-event-source'; +import type { PromptItemProps, SuggestionErrorCode } from '../../types'; export type SuggestionErrorProps = { /* @@ -50,12 +50,6 @@ type useAiSuggestionsOptions = { */ autoRequest?: boolean; - /* - * The post ID. - * It's value, when defined, will be passed to the askQuestion function. - */ - postId?: number; - /** * AskQuestion options. */ @@ -178,7 +172,6 @@ export default function useAiSuggestions( { prompt, autoRequest = false, askQuestionOptions = {}, - postId, onSuggestion, onDone, onError, @@ -257,18 +250,8 @@ export default function useAiSuggestions( { // Set the request status. setRequestingState( 'requesting' ); - const options = { - ...askQuestionOptions, - }; - - // Pass the post ID to the askQuestion function, when defined. - if ( postId ) { - debug( 'Post ID: %s', postId ); - options.postId = postId; - } - try { - eventSourceRef.current = await askQuestion( promptArg, options ); + eventSourceRef.current = await askQuestion( promptArg, askQuestionOptions ); if ( ! eventSourceRef?.current ) { return; @@ -295,7 +278,6 @@ export default function useAiSuggestions( { } }, [ - postId, handleDone, handleErrorQuotaExceededError, handleUnclearPromptError, diff --git a/src/suggestions-event-source/index.ts b/src/suggestions-event-source/index.ts index 62afd68..5bb47b0 100644 --- a/src/suggestions-event-source/index.ts +++ b/src/suggestions-event-source/index.ts @@ -68,7 +68,17 @@ export default class SuggestionsEventSource extends EventTarget { token, options = {}, }: SuggestionsEventSourceConstructorArgs ) { - const bodyData = { post_id: options?.postId, messages: [], question: '', feature: '' }; + const bodyData: { + post_id?: number; + messages?: PromptItemProps[]; + question?: string; + feature?: string; + } = {}; + + // Populate body data with post id + if ( options?.postId ) { + bodyData.post_id = options.postId; + } // If the url is not provided, we use the default one if ( ! url ) {