Skip to content

Commit

Permalink
AI Client: handle properly passing the post_id parameter to endpoint …
Browse files Browse the repository at this point in the history
…(#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
  • Loading branch information
retrofox authored and matticbot committed Jul 27, 2023
1 parent 9b8d588 commit d5b0f34
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion src/ask-question/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function askQuestion(
): Promise< SuggestionsEventSource >
```

## Parameters
<h2 id="ask-question-parameters">Parameters</h2>

- `question` (**string** | **PromptItemProps[]**):
- The question to ask.
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/use-ai-suggestions/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 4 additions & 22 deletions src/hooks/use-ai-suggestions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/*
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -178,7 +172,6 @@ export default function useAiSuggestions( {
prompt,
autoRequest = false,
askQuestionOptions = {},
postId,
onSuggestion,
onDone,
onError,
Expand Down Expand Up @@ -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;
Expand All @@ -295,7 +278,6 @@ export default function useAiSuggestions( {
}
},
[
postId,
handleDone,
handleErrorQuotaExceededError,
handleUnclearPromptError,
Expand Down
12 changes: 11 additions & 1 deletion src/suggestions-event-source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit d5b0f34

Please sign in to comment.