@automattic/jetpack-ai-client 0.14.5 → 0.14.6

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,10 @@ 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.14.6] - 2024-07-15
9
+ ### Added
10
+ - AI Client: Filter suggestions starting with llama artifacts [#38208]
11
+
8
12
  ## [0.14.5] - 2024-07-08
9
13
  ### Changed
10
14
  - Updated package dependencies. [#38132]
@@ -347,6 +351,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
347
351
  - Updated package dependencies. [#31659]
348
352
  - Updated package dependencies. [#31785]
349
353
 
354
+ [0.14.6]: https://github.com/Automattic/jetpack-ai-client/compare/v0.14.5...v0.14.6
350
355
  [0.14.5]: https://github.com/Automattic/jetpack-ai-client/compare/v0.14.4...v0.14.5
351
356
  [0.14.4]: https://github.com/Automattic/jetpack-ai-client/compare/v0.14.3...v0.14.4
352
357
  [0.14.3]: https://github.com/Automattic/jetpack-ai-client/compare/v0.14.2...v0.14.3
@@ -41,6 +41,13 @@ type useAiSuggestionsProps = {
41
41
  * @returns {RequestingErrorProps} The error data.
42
42
  */
43
43
  export declare function getErrorData(errorCode: SuggestionErrorCode): RequestingErrorProps;
44
+ /**
45
+ * Remove the llama artifact from a suggestion.
46
+ *
47
+ * @param {string} suggestion - The suggestion.
48
+ * @returns {string} The suggestion without the llama artifact.
49
+ */
50
+ export declare function removeLlamaArtifact(suggestion: string): string;
44
51
  /**
45
52
  * React custom hook to get suggestions from AI,
46
53
  * by hitting the query endpoint.
@@ -55,6 +55,15 @@ export function getErrorData(errorCode) {
55
55
  };
56
56
  }
57
57
  }
58
+ /**
59
+ * Remove the llama artifact from a suggestion.
60
+ *
61
+ * @param {string} suggestion - The suggestion.
62
+ * @returns {string} The suggestion without the llama artifact.
63
+ */
64
+ export function removeLlamaArtifact(suggestion) {
65
+ return suggestion.replace(/^<\|start_header_id\|>assistant<\|end_header_id\|>[\n]+/, '');
66
+ }
58
67
  /**
59
68
  * React custom hook to get suggestions from AI,
60
69
  * by hitting the query endpoint.
@@ -75,8 +84,12 @@ export default function useAiSuggestions({ prompt, autoRequest = false, askQuest
75
84
  * @returns {void}
76
85
  */
77
86
  const handleSuggestion = useCallback((event) => {
78
- setSuggestion(event?.detail);
79
- onSuggestion?.(event?.detail);
87
+ const partialSuggestion = removeLlamaArtifact(event?.detail);
88
+ if (!partialSuggestion) {
89
+ return;
90
+ }
91
+ setSuggestion(partialSuggestion);
92
+ onSuggestion?.(partialSuggestion);
80
93
  }, [onSuggestion]);
81
94
  /**
82
95
  * onDone function handler.
@@ -86,7 +99,8 @@ export default function useAiSuggestions({ prompt, autoRequest = false, askQuest
86
99
  */
87
100
  const handleDone = useCallback((event) => {
88
101
  closeEventSource();
89
- onDone?.(event?.detail);
102
+ const fullSuggestion = removeLlamaArtifact(event?.detail);
103
+ onDone?.(fullSuggestion);
90
104
  setRequestingState('done');
91
105
  }, [onDone]);
92
106
  const handleAnyError = useCallback((event) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@automattic/jetpack-ai-client",
4
- "version": "0.14.5",
4
+ "version": "0.14.6",
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": {
@@ -190,6 +190,16 @@ export function getErrorData( errorCode: SuggestionErrorCode ): RequestingErrorP
190
190
  }
191
191
  }
192
192
 
193
+ /**
194
+ * Remove the llama artifact from a suggestion.
195
+ *
196
+ * @param {string} suggestion - The suggestion.
197
+ * @returns {string} The suggestion without the llama artifact.
198
+ */
199
+ export function removeLlamaArtifact( suggestion: string ): string {
200
+ return suggestion.replace( /^<\|start_header_id\|>assistant<\|end_header_id\|>[\n]+/, '' );
201
+ }
202
+
193
203
  /**
194
204
  * React custom hook to get suggestions from AI,
195
205
  * by hitting the query endpoint.
@@ -224,8 +234,14 @@ export default function useAiSuggestions( {
224
234
  */
225
235
  const handleSuggestion = useCallback(
226
236
  ( event: CustomEvent ) => {
227
- setSuggestion( event?.detail );
228
- onSuggestion?.( event?.detail );
237
+ const partialSuggestion = removeLlamaArtifact( event?.detail );
238
+
239
+ if ( ! partialSuggestion ) {
240
+ return;
241
+ }
242
+
243
+ setSuggestion( partialSuggestion );
244
+ onSuggestion?.( partialSuggestion );
229
245
  },
230
246
  [ onSuggestion ]
231
247
  );
@@ -239,7 +255,10 @@ export default function useAiSuggestions( {
239
255
  const handleDone = useCallback(
240
256
  ( event: CustomEvent ) => {
241
257
  closeEventSource();
242
- onDone?.( event?.detail );
258
+
259
+ const fullSuggestion = removeLlamaArtifact( event?.detail );
260
+
261
+ onDone?.( fullSuggestion );
243
262
  setRequestingState( 'done' );
244
263
  },
245
264
  [ onDone ]