@automattic/jetpack-ai-client 0.9.0 → 0.10.0

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.10.0] - 2024-03-18
9
+ ### Added
10
+ - Add image generator hook [#36415]
11
+
8
12
  ## [0.9.0] - 2024-03-12
9
13
  ### Changed
10
14
  - Fix typescript errors [#35904]
@@ -255,6 +259,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
255
259
  - Updated package dependencies. [#31659]
256
260
  - Updated package dependencies. [#31785]
257
261
 
262
+ [0.10.0]: https://github.com/Automattic/jetpack-ai-client/compare/v0.9.0...v0.10.0
258
263
  [0.9.0]: https://github.com/Automattic/jetpack-ai-client/compare/v0.8.2...v0.9.0
259
264
  [0.8.2]: https://github.com/Automattic/jetpack-ai-client/compare/v0.8.1...v0.8.2
260
265
  [0.8.1]: https://github.com/Automattic/jetpack-ai-client/compare/v0.8.0...v0.8.1
@@ -0,0 +1,10 @@
1
+ declare const useImageGenerator: () => {
2
+ generateImage: ({ feature, }: {
3
+ feature: string;
4
+ }) => Promise<{
5
+ data: Array<{
6
+ url: string;
7
+ }>;
8
+ }>;
9
+ };
10
+ export default useImageGenerator;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import debugFactory from 'debug';
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import requestJwt from '../../jwt/index.js';
9
+ const debug = debugFactory('ai-client:use-image-generator');
10
+ const useImageGenerator = () => {
11
+ const generateImage = async function ({ feature, }) {
12
+ let token = '';
13
+ try {
14
+ token = (await requestJwt()).token;
15
+ }
16
+ catch (error) {
17
+ debug('Error getting token: %o', error);
18
+ return Promise.reject(error);
19
+ }
20
+ try {
21
+ debug('Generating image');
22
+ // TODO: Find a proper prompt for the image generation
23
+ const imageGenerationPrompt = ``;
24
+ const URL = 'https://public-api.wordpress.com/wpcom/v2/jetpack-ai-image';
25
+ const body = {
26
+ prompt: imageGenerationPrompt,
27
+ response_format: 'url',
28
+ feature,
29
+ };
30
+ const headers = {
31
+ Authorization: `Bearer ${token}`,
32
+ };
33
+ const data = await fetch(URL, {
34
+ method: 'POST',
35
+ headers,
36
+ body: JSON.stringify(body),
37
+ }).then(response => response.json());
38
+ return data;
39
+ }
40
+ catch (error) {
41
+ return;
42
+ }
43
+ };
44
+ return {
45
+ generateImage,
46
+ };
47
+ };
48
+ export default useImageGenerator;
package/build/index.d.ts CHANGED
@@ -7,6 +7,7 @@ export { default as useMediaRecording } from './hooks/use-media-recording/index.
7
7
  export { default as useAudioTranscription } from './hooks/use-audio-transcription/index.js';
8
8
  export { default as useTranscriptionPostProcessing } from './hooks/use-transcription-post-processing/index.js';
9
9
  export { default as useAudioValidation } from './hooks/use-audio-validation/index.js';
10
+ export { default as useImageGenerator } from './hooks/use-image-generator/index.js';
10
11
  export * from './icons/index.js';
11
12
  export * from './components/index.js';
12
13
  export * from './data-flow/index.js';
package/build/index.js CHANGED
@@ -13,6 +13,7 @@ export { default as useMediaRecording } from './hooks/use-media-recording/index.
13
13
  export { default as useAudioTranscription } from './hooks/use-audio-transcription/index.js';
14
14
  export { default as useTranscriptionPostProcessing } from './hooks/use-transcription-post-processing/index.js';
15
15
  export { default as useAudioValidation } from './hooks/use-audio-validation/index.js';
16
+ export { default as useImageGenerator } from './hooks/use-image-generator/index.js';
16
17
  /*
17
18
  * Components: Icons
18
19
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "private": false,
3
3
  "name": "@automattic/jetpack-ai-client",
4
- "version": "0.9.0",
4
+ "version": "0.10.0",
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": {
@@ -0,0 +1,62 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import debugFactory from 'debug';
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import requestJwt from '../../jwt/index.js';
9
+
10
+ const debug = debugFactory( 'ai-client:use-image-generator' );
11
+
12
+ const useImageGenerator = () => {
13
+ const generateImage = async function ( {
14
+ feature,
15
+ }: {
16
+ feature: string;
17
+ } ): Promise< { data: Array< { url: string } > } > {
18
+ let token = '';
19
+
20
+ try {
21
+ token = ( await requestJwt() ).token;
22
+ } catch ( error ) {
23
+ debug( 'Error getting token: %o', error );
24
+ return Promise.reject( error );
25
+ }
26
+
27
+ try {
28
+ debug( 'Generating image' );
29
+
30
+ // TODO: Find a proper prompt for the image generation
31
+ const imageGenerationPrompt = ``;
32
+
33
+ const URL = 'https://public-api.wordpress.com/wpcom/v2/jetpack-ai-image';
34
+
35
+ const body = {
36
+ prompt: imageGenerationPrompt,
37
+ response_format: 'url',
38
+ feature,
39
+ };
40
+
41
+ const headers = {
42
+ Authorization: `Bearer ${ token }`,
43
+ };
44
+
45
+ const data = await fetch( URL, {
46
+ method: 'POST',
47
+ headers,
48
+ body: JSON.stringify( body ),
49
+ } ).then( response => response.json() );
50
+
51
+ return data as { data: { url: string }[] };
52
+ } catch ( error ) {
53
+ return;
54
+ }
55
+ };
56
+
57
+ return {
58
+ generateImage,
59
+ };
60
+ };
61
+
62
+ export default useImageGenerator;
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export { default as useMediaRecording } from './hooks/use-media-recording/index.
14
14
  export { default as useAudioTranscription } from './hooks/use-audio-transcription/index.js';
15
15
  export { default as useTranscriptionPostProcessing } from './hooks/use-transcription-post-processing/index.js';
16
16
  export { default as useAudioValidation } from './hooks/use-audio-validation/index.js';
17
+ export { default as useImageGenerator } from './hooks/use-image-generator/index.js';
17
18
 
18
19
  /*
19
20
  * Components: Icons