@automattic/jetpack-ai-client 0.1.14 → 0.1.15

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,11 @@ 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.15] - 2023-11-13
9
+ ### Changed
10
+ - Prevented dispatching the `done` event for JETPACK_AI_ERROR. [#34051]
11
+ - Ensured the unclear prompt error is dispatched only once per request. [#34025]
12
+
8
13
  ## [0.1.14] - 2023-11-03
9
14
 
10
15
  ## [0.1.13] - 2023-10-23
@@ -153,6 +158,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
153
158
  - Updated package dependencies. [#31659]
154
159
  - Updated package dependencies. [#31785]
155
160
 
161
+ [0.1.15]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.14...v0.1.15
156
162
  [0.1.14]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.13...v0.1.14
157
163
  [0.1.13]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.12...v0.1.13
158
164
  [0.1.12]: https://github.com/Automattic/jetpack-ai-client/compare/v0.1.11...v0.1.12
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.14",
4
+ "version": "0.1.15",
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": {
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@automattic/jetpack-base-styles": "^0.6.11",
36
- "@automattic/jetpack-connection": "^0.30.5",
37
- "@automattic/jetpack-shared-extension-utils": "^0.12.5",
36
+ "@automattic/jetpack-connection": "^0.30.6",
37
+ "@automattic/jetpack-shared-extension-utils": "^0.13.0",
38
38
  "@microsoft/fetch-event-source": "2.0.1",
39
39
  "@wordpress/api-fetch": "6.41.0",
40
40
  "@wordpress/block-editor": "12.12.0",
@@ -72,6 +72,9 @@ export default class SuggestionsEventSource extends EventTarget {
72
72
  isPromptClear: boolean;
73
73
  controller: AbortController;
74
74
 
75
+ // Flag to detect if the unclear prompt event was already dispatched
76
+ errorUnclearPromptTriggered: boolean;
77
+
75
78
  constructor( data: SuggestionsEventSourceConstructorArgs ) {
76
79
  super();
77
80
  this.fullMessage = '';
@@ -157,6 +160,9 @@ export default class SuggestionsEventSource extends EventTarget {
157
160
  bodyData.model = options.model;
158
161
  }
159
162
 
163
+ // Clean the unclear prompt trigger flag
164
+ this.errorUnclearPromptTriggered = false;
165
+
160
166
  await fetchEventSource( url, {
161
167
  openWhenHidden: true,
162
168
  method: 'POST',
@@ -257,8 +263,19 @@ export default class SuggestionsEventSource extends EventTarget {
257
263
  */
258
264
  const replacedMessage = this.fullMessage.replace( /__|(\*\*)/g, '' );
259
265
  if ( replacedMessage.startsWith( 'JETPACK_AI_ERROR' ) ) {
266
+ /*
267
+ * Check if the unclear prompt event was already dispatched,
268
+ * to ensure that it is dispatched only once per request.
269
+ */
270
+ if ( this.errorUnclearPromptTriggered ) {
271
+ return;
272
+ }
273
+ this.errorUnclearPromptTriggered = true;
274
+
260
275
  // The unclear prompt marker was found, so we dispatch an error event
261
276
  this.dispatchEvent( new CustomEvent( ERROR_UNCLEAR_PROMPT ) );
277
+ debug( 'Unclear error prompt dispatched' );
278
+
262
279
  this.dispatchEvent(
263
280
  new CustomEvent( ERROR_RESPONSE, {
264
281
  detail: getErrorData( ERROR_UNCLEAR_PROMPT ),
@@ -279,6 +296,14 @@ export default class SuggestionsEventSource extends EventTarget {
279
296
 
280
297
  processEvent( e: EventSourceMessage ) {
281
298
  if ( e.data === '[DONE]' ) {
299
+ /*
300
+ * Check if the unclear prompt event was already dispatched,
301
+ * to ensure that it is dispatched only once per request.
302
+ */
303
+ if ( this.errorUnclearPromptTriggered ) {
304
+ return;
305
+ }
306
+
282
307
  if ( this.fullMessage.length ) {
283
308
  // Dispatch an event with the full content
284
309
  this.dispatchEvent( new CustomEvent( 'done', { detail: this.fullMessage } ) );