@automattic/jetpack-ai-client 0.1.3 → 0.1.5

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/CHANGELOG.md CHANGED
@@ -5,6 +5,30 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.1.5] - 2023-08-28
9
+ ### Added
10
+ - AI Client: add mic icon [#32665]
11
+
12
+ ### Changed
13
+ - AI Assistant: Change messages to turn content optional and start supporting a context property. [#32495]
14
+ - AI Extension: Add showClearButton prop to AIControl component and fix names [#32682]
15
+ - AI Extension: Specify input background color [#32628]
16
+ - Updated package dependencies. [#32605]
17
+
18
+ ## [0.1.4] - 2023-08-14
19
+ ### Added
20
+ - AI Client: Add border-box in AIControl. [#32419]
21
+ - AI Client: Export AiStatusIndicator. [#32397]
22
+ - AI Client: Import base styles in the AI status indicator component. [#32396]
23
+ - AI Control: Forward ref to consumer. [#32400]
24
+ - AI Control: Import jetpack-base-styles. [#32376]
25
+
26
+ ### Changed
27
+ - AI Client: Expose stopSuggestion function on useAiSuggestions hook so the consumer can stop a suggestion in the middle. [#32382]
28
+
29
+ ### Removed
30
+ - AI Client: Remove redundant switch case [#32405]
31
+
8
32
  ## [0.1.3] - 2023-08-09
9
33
  ### Added
10
34
  - AI Client: Introduce disabled prop in AI Control. [#32326]
@@ -68,6 +92,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
68
92
  - Updated package dependencies. [#31659]
69
93
  - Updated package dependencies. [#31785]
70
94
 
95
+ [0.1.5]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.4...v0.1.5
96
+ [0.1.4]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.3...v0.1.4
71
97
  [0.1.3]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.2...v0.1.3
72
98
  [0.1.2]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.1...v0.1.2
73
99
  [0.1.1]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.0...v0.1.1
package/index.ts CHANGED
@@ -18,7 +18,7 @@ export * from './src/icons';
18
18
  /*
19
19
  * Components
20
20
  */
21
- export { default as AIControl } from './src/components/ai-control';
21
+ export * from './src/components';
22
22
 
23
23
  /*
24
24
  * Contexts
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@automattic/jetpack-ai-client",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "description": "A JS client for consuming Jetpack AI services",
6
6
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/ai-client/#readme",
7
7
  "bugs": {
@@ -32,8 +32,9 @@
32
32
  ".": "./index.ts"
33
33
  },
34
34
  "dependencies": {
35
- "@automattic/jetpack-connection": "workspace:*",
36
- "@automattic/jetpack-shared-extension-utils": "^0.11.1",
35
+ "@automattic/jetpack-base-styles": "^0.6.6",
36
+ "@automattic/jetpack-connection": "^0.29.8",
37
+ "@automattic/jetpack-shared-extension-utils": "^0.11.3",
37
38
  "@microsoft/fetch-event-source": "2.0.1",
38
39
  "@wordpress/api-fetch": "6.35.0",
39
40
  "@wordpress/block-editor": "12.6.0",
@@ -7,9 +7,10 @@
7
7
  - `placeholder` (**string**) (Optional): Placeholder text for the input field. Default value is `''`.
8
8
  - `showAccept` (**boolean**) (Optional): Determines if the accept button is shown. Default value is `false`.
9
9
  - `acceptLabel` (**string**) (Optional): Label text for the accept button. Default value is `'Accept'`.
10
- - `showButtonsLabel` (**boolean**) (Optional): Determines if button labels are shown. Default value is `true`.
11
- - `isOpaque` (**boolean**) (Optional): Controls the opacity of the component. Default value is `false`.
10
+ - `showButtonLabels` (**boolean**) (Optional): Determines if button labels are shown. Default value is `true`.
11
+ - `isTransparent` (**boolean**) (Optional): Controls the opacity of the component. Default value is `false`.
12
12
  - `state` (**RequestingStateProp**) (Optional): Determines the state of the component. Default value is `'init'`.
13
+ - `showClearButton` (**boolean**) (Optional): Determines if the clear button is shown when the input has a value. Default value is `true`.
13
14
  - `onChange` (**Function**) (Optional): Handler for input change. Default action is no operation.
14
15
  - `onSend` (**Function**) (Optional): Handler to send a request. Default action is no operation.
15
16
  - `onStop` (**Function**) (Optional): Handler to stop a request. Default action is no operation.
@@ -4,10 +4,11 @@
4
4
  import { PlainText } from '@wordpress/block-editor';
5
5
  import { Button } from '@wordpress/components';
6
6
  import { useKeyboardShortcut } from '@wordpress/compose';
7
- import { useRef } from '@wordpress/element';
7
+ import { forwardRef, useImperativeHandle, useRef } from '@wordpress/element';
8
8
  import { __ } from '@wordpress/i18n';
9
9
  import { Icon, closeSmall, check, arrowUp } from '@wordpress/icons';
10
10
  import classNames from 'classnames';
11
+ import React from 'react';
11
12
  /**
12
13
  * Internal dependencies
13
14
  */
@@ -25,51 +26,61 @@ const noop = () => {};
25
26
  /**
26
27
  * AI Control component.
27
28
  *
28
- * @param {object} props - component props
29
- * @param {boolean} props.disabled - is disabled
30
- * @param {string} props.value - input value
31
- * @param {string} props.placeholder - input placeholder
32
- * @param {boolean} props.showAccept - show accept button
33
- * @param {string} props.acceptLabel - accept button label
34
- * @param {boolean} props.showButtonsLabel - show buttons label
35
- * @param {boolean} props.isOpaque - is opaque
36
- * @param {string} props.state - requesting state
37
- * @param {Function} props.onChange - input change handler
38
- * @param {Function} props.onSend - send request handler
39
- * @param {Function} props.onStop - stop request handler
40
- * @param {Function} props.onAccept - accept handler
29
+ * @param {object} props - Component props
30
+ * @param {boolean} props.disabled - Input disabled state
31
+ * @param {string} props.value - The input value
32
+ * @param {string} props.placeholder - The input placeholder
33
+ * @param {boolean} props.showAccept - Whether to show the accept button
34
+ * @param {string} props.acceptLabel - The accept button label
35
+ * @param {boolean} props.showButtonLabels - Whether to show the button labels
36
+ * @param {boolean} props.isTransparent - Whether the component has low opacity
37
+ * @param {string} props.state - The request state
38
+ * @param {boolean} props.showClearButton - Whether to show the clear button when the input has a value
39
+ * @param {Function} props.onChange - Input change handler
40
+ * @param {Function} props.onSend - Request send handler
41
+ * @param {Function} props.onStop - Request stop handler
42
+ * @param {Function} props.onAccept - Response accept handler
43
+ * @param {object} ref - Auto injected ref from react
41
44
  * @returns {object} - AI Control component
42
45
  */
43
- export default function AIControl( {
44
- disabled = false,
45
- value = '',
46
- placeholder = '',
47
- showAccept = false,
48
- acceptLabel = __( 'Accept', 'jetpack-ai-client' ),
49
- showButtonsLabel = true,
50
- isOpaque = false,
51
- state = 'init',
52
- onChange = noop,
53
- onSend = noop,
54
- onStop = noop,
55
- onAccept = noop,
56
- }: {
57
- disabled?: boolean;
58
- value: string;
59
- placeholder?: string;
60
- showAccept?: boolean;
61
- acceptLabel?: string;
62
- showButtonsLabel?: boolean;
63
- isOpaque?: boolean;
64
- state?: RequestingStateProp;
65
- onChange?: ( newValue: string ) => void;
66
- onSend?: ( currentValue: string ) => void;
67
- onStop?: () => void;
68
- onAccept?: () => void;
69
- } ) {
46
+ export function AIControl(
47
+ {
48
+ disabled = false,
49
+ value = '',
50
+ placeholder = '',
51
+ showAccept = false,
52
+ acceptLabel = __( 'Accept', 'jetpack-ai-client' ),
53
+ showButtonLabels = true,
54
+ isTransparent = false,
55
+ state = 'init',
56
+ showClearButton = true,
57
+ onChange = noop,
58
+ onSend = noop,
59
+ onStop = noop,
60
+ onAccept = noop,
61
+ }: {
62
+ disabled?: boolean;
63
+ value: string;
64
+ placeholder?: string;
65
+ showAccept?: boolean;
66
+ acceptLabel?: string;
67
+ showButtonLabels?: boolean;
68
+ isTransparent?: boolean;
69
+ state?: RequestingStateProp;
70
+ showClearButton?: boolean;
71
+ onChange?: ( newValue: string ) => void;
72
+ onSend?: ( currentValue: string ) => void;
73
+ onStop?: () => void;
74
+ onAccept?: () => void;
75
+ },
76
+ ref
77
+ ) {
70
78
  const promptUserInputRef = useRef( null );
71
79
  const loading = state === 'requesting' || state === 'suggesting';
72
- const showGuideLine = ! ( loading || disabled || value?.length || isOpaque );
80
+ const showGuideLine = ! ( loading || disabled || value?.length || isTransparent );
81
+
82
+ // Pass the ref to forwardRef.
83
+ useImperativeHandle( ref, () => promptUserInputRef.current );
73
84
 
74
85
  useKeyboardShortcut(
75
86
  'mod+enter',
@@ -95,14 +106,14 @@ export default function AIControl( {
95
106
  );
96
107
 
97
108
  const actionButtonClasses = classNames( 'jetpack-components-ai-control__controls-prompt_button', {
98
- 'has-label': showButtonsLabel,
109
+ 'has-label': showButtonLabels,
99
110
  } );
100
111
 
101
112
  return (
102
113
  <div className="jetpack-components-ai-control__container">
103
114
  <div
104
115
  className={ classNames( 'jetpack-components-ai-control__wrapper', {
105
- 'is-opaque': isOpaque,
116
+ 'is-transparent': isTransparent,
106
117
  } ) }
107
118
  >
108
119
  <AiStatusIndicator state={ state } />
@@ -118,7 +129,7 @@ export default function AIControl( {
118
129
  />
119
130
  </div>
120
131
 
121
- { value?.length > 0 && (
132
+ { value?.length > 0 && showClearButton && (
122
133
  <Button
123
134
  icon={ closeSmall }
124
135
  className="jetpack-components-ai-control__clear"
@@ -136,7 +147,7 @@ export default function AIControl( {
136
147
  label={ __( 'Send request', 'jetpack-ai-client' ) }
137
148
  >
138
149
  <Icon icon={ arrowUp } />
139
- { showButtonsLabel && __( 'Send', 'jetpack-ai-client' ) }
150
+ { showButtonLabels && __( 'Send', 'jetpack-ai-client' ) }
140
151
  </Button>
141
152
  ) : (
142
153
  <Button
@@ -146,7 +157,7 @@ export default function AIControl( {
146
157
  label={ __( 'Stop request', 'jetpack-ai-client' ) }
147
158
  >
148
159
  <Icon icon={ closeSmall } />
149
- { showButtonsLabel && __( 'Stop', 'jetpack-ai-client' ) }
160
+ { showButtonLabels && __( 'Stop', 'jetpack-ai-client' ) }
150
161
  </Button>
151
162
  ) }
152
163
  </div>
@@ -160,7 +171,7 @@ export default function AIControl( {
160
171
  label={ acceptLabel }
161
172
  >
162
173
  <Icon icon={ check } />
163
- { showButtonsLabel && acceptLabel }
174
+ { showButtonLabels && acceptLabel }
164
175
  </Button>
165
176
  </div>
166
177
  ) }
@@ -169,3 +180,5 @@ export default function AIControl( {
169
180
  </div>
170
181
  );
171
182
  }
183
+
184
+ export default forwardRef( AIControl );
@@ -52,10 +52,10 @@ const Template = args => {
52
52
 
53
53
  const DefaultArgs = {
54
54
  loading: false,
55
- isOpaque: false,
55
+ isTransparent: false,
56
56
  placeholder: '',
57
57
  requestingState: 'init',
58
- showButtonsLabel: true,
58
+ showButtonLabels: true,
59
59
  showAccept: false,
60
60
  acceptLabel: 'Accept',
61
61
  onChange: action( 'onChange' ),
@@ -1,3 +1,5 @@
1
+ @import '@automattic/jetpack-base-styles/root-variables';
2
+
1
3
  // AI CONTROL
2
4
 
3
5
  .jetpack-components-ai-control__container {
@@ -13,8 +15,9 @@
13
15
  padding: 12px 14px;
14
16
  gap: 8px;
15
17
  align-items: center;
18
+ box-sizing: border-box;
16
19
 
17
- &.is-opaque {
20
+ &.is-transparent {
18
21
  opacity: 0.4;
19
22
  }
20
23
  }
@@ -26,6 +29,7 @@
26
29
  width: 100%;
27
30
 
28
31
  textarea.jetpack-components-ai-control__input {
32
+ background-color: var( --jp-white );
29
33
  width: 100%;
30
34
  min-height: 20px;
31
35
  border-radius: 2px;
@@ -1,3 +1,5 @@
1
+ @import '@automattic/jetpack-base-styles/style';
2
+
1
3
  .jetpack-ai-status-indicator__icon-wrapper {
2
4
  color: var( --jp-green-60 );
3
5
  height: 24px;
@@ -0,0 +1,2 @@
1
+ export { default as AIControl } from './ai-control';
2
+ export { default as AiStatusIndicator } from './ai-status-indicator';
@@ -33,6 +33,11 @@ export type AiDataContextProps = {
33
33
  */
34
34
  requestSuggestion: ( prompt: PromptProp, options?: AskQuestionOptionsArgProps ) => void;
35
35
 
36
+ /*
37
+ * Stop suggestion function
38
+ */
39
+ stopSuggestion: () => void;
40
+
36
41
  /*
37
42
  * The Suggestions Event Source instance
38
43
  */
@@ -25,6 +25,7 @@ const withAiDataProvider = createHigherOrderComponent( ( WrappedComponent: React
25
25
  error: requestingError,
26
26
  requestingState,
27
27
  request: requestSuggestion,
28
+ stopSuggestion,
28
29
  eventSource,
29
30
  } = useAiSuggestions();
30
31
 
@@ -37,8 +38,16 @@ const withAiDataProvider = createHigherOrderComponent( ( WrappedComponent: React
37
38
  eventSource,
38
39
 
39
40
  requestSuggestion,
41
+ stopSuggestion,
40
42
  } ),
41
- [ suggestion, requestingError, requestingState, eventSource, requestSuggestion ]
43
+ [
44
+ suggestion,
45
+ requestingError,
46
+ requestingState,
47
+ eventSource,
48
+ requestSuggestion,
49
+ stopSuggestion,
50
+ ]
42
51
  );
43
52
 
44
53
  return (
@@ -97,6 +97,11 @@ type useAiSuggestionsProps = {
97
97
  * The request handler.
98
98
  */
99
99
  request: ( prompt: PromptProp, options?: AskQuestionOptionsArgProps ) => Promise< void >;
100
+
101
+ /*
102
+ * The handler to stop a suggestion.
103
+ */
104
+ stopSuggestion: () => void;
100
105
  };
101
106
 
102
107
  const debug = debugFactory( 'jetpack-ai-client:use-suggestion' );
@@ -140,14 +145,6 @@ export function getErrorData( errorCode: SuggestionErrorCode ): RequestingErrorP
140
145
  severity: 'info',
141
146
  };
142
147
  case ERROR_NETWORK:
143
- return {
144
- code: ERROR_NETWORK,
145
- message: __(
146
- 'It was not possible to process your request. Mind trying again?',
147
- 'jetpack-ai-client'
148
- ),
149
- severity: 'info',
150
- };
151
148
  default:
152
149
  return {
153
150
  code: ERROR_NETWORK,
@@ -297,6 +294,46 @@ export default function useAiSuggestions( {
297
294
  ]
298
295
  );
299
296
 
297
+ /**
298
+ * Stop suggestion handler.
299
+ *
300
+ * @returns {void}
301
+ */
302
+ const stopSuggestion = useCallback( () => {
303
+ if ( ! eventSourceRef?.current ) {
304
+ return;
305
+ }
306
+
307
+ // Alias
308
+ const eventSource = eventSourceRef?.current;
309
+
310
+ // Close the connection.
311
+ eventSource.close();
312
+
313
+ // Clean up the event listeners.
314
+ eventSource.removeEventListener( 'suggestion', handleSuggestion );
315
+
316
+ eventSource.removeEventListener( ERROR_QUOTA_EXCEEDED, handleErrorQuotaExceededError );
317
+ eventSource.removeEventListener( ERROR_UNCLEAR_PROMPT, handleUnclearPromptError );
318
+ eventSource.removeEventListener( ERROR_SERVICE_UNAVAILABLE, handleServiceUnavailableError );
319
+ eventSource.removeEventListener( ERROR_MODERATION, handleModerationError );
320
+ eventSource.removeEventListener( ERROR_NETWORK, handleNetworkError );
321
+
322
+ eventSource.removeEventListener( 'done', handleDone );
323
+
324
+ // Set requesting state to done since the suggestion stopped.
325
+ setRequestingState( 'done' );
326
+ }, [
327
+ eventSourceRef,
328
+ handleSuggestion,
329
+ handleErrorQuotaExceededError,
330
+ handleUnclearPromptError,
331
+ handleServiceUnavailableError,
332
+ handleModerationError,
333
+ handleNetworkError,
334
+ handleDone,
335
+ ] );
336
+
300
337
  // Request suggestions automatically when ready.
301
338
  useEffect( () => {
302
339
  // Check if there is a prompt to request.
@@ -310,38 +347,10 @@ export default function useAiSuggestions( {
310
347
  }
311
348
 
312
349
  return () => {
313
- if ( ! eventSourceRef?.current ) {
314
- return;
315
- }
316
-
317
- // Alias
318
- const eventSource = eventSourceRef.current;
319
-
320
- // Close the connection.
321
- eventSource.close();
322
-
323
- // Clean up the event listeners.
324
- eventSource.removeEventListener( 'suggestion', handleSuggestion );
325
-
326
- eventSource.removeEventListener( ERROR_QUOTA_EXCEEDED, handleErrorQuotaExceededError );
327
- eventSource.removeEventListener( ERROR_UNCLEAR_PROMPT, handleUnclearPromptError );
328
- eventSource.removeEventListener( ERROR_SERVICE_UNAVAILABLE, handleServiceUnavailableError );
329
- eventSource.removeEventListener( ERROR_MODERATION, handleModerationError );
330
- eventSource.removeEventListener( ERROR_NETWORK, handleNetworkError );
331
-
332
- eventSource.removeEventListener( 'done', handleDone );
350
+ // Stop the suggestion if the component unmounts.
351
+ stopSuggestion();
333
352
  };
334
- }, [
335
- autoRequest,
336
- handleDone,
337
- handleErrorQuotaExceededError,
338
- handleModerationError,
339
- handleServiceUnavailableError,
340
- handleSuggestion,
341
- handleUnclearPromptError,
342
- prompt,
343
- request,
344
- ] );
353
+ }, [ autoRequest, prompt, request, stopSuggestion ] );
345
354
 
346
355
  return {
347
356
  // Data
@@ -349,8 +358,9 @@ export default function useAiSuggestions( {
349
358
  error,
350
359
  requestingState,
351
360
 
352
- // Request handler
361
+ // Request/stop handlers
353
362
  request,
363
+ stopSuggestion,
354
364
 
355
365
  // SuggestionsEventSource
356
366
  eventSource: eventSourceRef.current,
@@ -1,3 +1,4 @@
1
1
  export { default as aiAssistantIcon } from './ai-assistant';
2
+ export { default as micIcon } from './mic';
2
3
  export { default as origamiPlaneIcon } from './origami-plane';
3
4
  export { default as speakToneIcon } from './speak-tone';
@@ -0,0 +1,19 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { SVG, Rect, Line, Path } from '@wordpress/components';
5
+ import React from 'react';
6
+
7
+ const mic = (
8
+ <SVG width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/SVG">
9
+ <Rect x="8.75" y="2.75" width="6.5" height="11.5" rx="3.25" stroke="black" stroke-width="1.5" />
10
+ <Line x1="12" y1="17" x2="12" y2="21" stroke="black" stroke-width="1.5" />
11
+ <Path
12
+ d="M18 11C18 11.7879 17.8448 12.5681 17.5433 13.2961C17.2417 14.0241 16.7998 14.6855 16.2426 15.2426C15.6855 15.7998 15.0241 16.2418 14.2961 16.5433C13.5681 16.8448 12.7879 17 12 17C11.2121 17 10.4319 16.8448 9.7039 16.5433C8.97595 16.2417 8.31451 15.7998 7.75736 15.2426C7.20021 14.6855 6.75825 14.0241 6.45672 13.2961C6.15519 12.5681 6 11.7879 6 11"
13
+ stroke="black"
14
+ stroke-width="1.5"
15
+ />
16
+ </SVG>
17
+ );
18
+
19
+ export default mic;
package/src/types.ts CHANGED
@@ -18,7 +18,8 @@ export type SuggestionErrorCode =
18
18
  */
19
19
  export type PromptItemProps = {
20
20
  role: 'system' | 'user' | 'assistant';
21
- content: string;
21
+ content?: string;
22
+ context?: object;
22
23
  };
23
24
 
24
25
  export type PromptMessagesProp = Array< PromptItemProps >;