@automattic/jetpack-ai-client 0.6.1 → 0.7.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 +11 -0
- package/build/api-fetch/index.d.ts +7 -0
- package/build/api-fetch/index.js +6 -0
- package/build/ask-question/index.d.ts +2 -2
- package/build/ask-question/index.js +1 -1
- package/build/audio-transcription/index.d.ts +8 -0
- package/build/audio-transcription/index.js +49 -0
- package/build/components/ai-control/index.d.ts +1 -1
- package/build/components/ai-control/index.js +2 -2
- package/build/components/ai-status-indicator/index.d.ts +1 -1
- package/build/components/audio-duration-display/index.d.ts +3 -2
- package/build/components/audio-duration-display/index.js +3 -14
- package/build/components/audio-duration-display/lib/media.d.ts +13 -14
- package/build/components/audio-duration-display/lib/media.js +7 -32
- package/build/components/index.d.ts +4 -4
- package/build/components/index.js +4 -4
- package/build/data-flow/context.d.ts +5 -5
- package/build/data-flow/index.d.ts +3 -3
- package/build/data-flow/index.js +3 -3
- package/build/data-flow/use-ai-context.d.ts +3 -3
- package/build/data-flow/use-ai-context.js +2 -2
- package/build/data-flow/with-ai-assistant-data.js +2 -2
- package/build/hooks/use-ai-suggestions/index.d.ts +4 -4
- package/build/hooks/use-ai-suggestions/index.js +2 -2
- package/build/hooks/use-audio-transcription/index.d.ts +24 -0
- package/build/hooks/use-audio-transcription/index.js +49 -0
- package/build/hooks/use-media-recording/index.d.ts +30 -12
- package/build/hooks/use-media-recording/index.js +106 -40
- package/build/hooks/use-transcription-post-processing/index.d.ts +30 -0
- package/build/hooks/use-transcription-post-processing/index.js +84 -0
- package/build/icons/index.d.ts +7 -7
- package/build/icons/index.js +7 -7
- package/build/icons/mic.js +2 -2
- package/build/icons/player-pause.js +1 -1
- package/build/index.d.ts +12 -9
- package/build/index.js +12 -9
- package/build/jwt/index.js +4 -1
- package/build/suggestions-event-source/index.d.ts +1 -1
- package/build/suggestions-event-source/index.js +3 -3
- package/build/types.d.ts +5 -2
- package/build/types.js +4 -0
- package/package.json +2 -2
- package/src/api-fetch/index.ts +13 -0
- package/src/ask-question/index.ts +2 -2
- package/src/audio-transcription/index.ts +67 -0
- package/src/components/ai-control/index.tsx +3 -3
- package/src/components/ai-status-indicator/index.tsx +1 -1
- package/src/components/audio-duration-display/index.tsx +6 -17
- package/src/components/audio-duration-display/lib/media.ts +17 -40
- package/src/components/index.ts +4 -4
- package/src/data-flow/context.tsx +5 -5
- package/src/data-flow/index.ts +3 -3
- package/src/data-flow/use-ai-context.ts +4 -4
- package/src/data-flow/with-ai-assistant-data.tsx +2 -2
- package/src/hooks/use-ai-suggestions/index.ts +6 -6
- package/src/hooks/use-audio-transcription/index.ts +81 -0
- package/src/hooks/use-media-recording/Readme.md +2 -2
- package/src/hooks/use-media-recording/index.ts +161 -58
- package/src/hooks/use-transcription-post-processing/index.ts +135 -0
- package/src/icons/index.ts +7 -7
- package/src/icons/mic.tsx +14 -16
- package/src/icons/player-pause.tsx +5 -5
- package/src/index.ts +12 -9
- package/src/jwt/index.ts +4 -1
- package/src/suggestions-event-source/index.ts +4 -4
- package/src/types.ts +19 -2
|
@@ -11,12 +11,19 @@ import { useRef, useState, useEffect, useCallback } from '@wordpress/element';
|
|
|
11
11
|
export default function useMediaRecording({ onDone, } = {}) {
|
|
12
12
|
// Reference to the media recorder instance
|
|
13
13
|
const mediaRecordRef = useRef(null);
|
|
14
|
-
// Recording state: `inactive`, `recording`, `paused`
|
|
14
|
+
// Recording state: `inactive`, `recording`, `paused`, `processing`, `error`
|
|
15
15
|
const [state, setState] = useState('inactive');
|
|
16
|
+
// reference to the paused state to be used in the `onDataAvailable` event listener,
|
|
17
|
+
// as the `mediaRecordRef.current.state` is already `inactive` when the recorder is stopped,
|
|
18
|
+
// and the event listener does not react to state changes
|
|
19
|
+
const isPaused = useRef(false);
|
|
20
|
+
const recordStartTimestamp = useRef(0);
|
|
21
|
+
const [duration, setDuration] = useState(0);
|
|
16
22
|
// The recorded blob
|
|
17
23
|
const [blob, setBlob] = useState(null);
|
|
18
24
|
// Store the recorded chunks
|
|
19
25
|
const recordedChunks = useRef([]).current;
|
|
26
|
+
const [error, setError] = useState(null);
|
|
20
27
|
/**
|
|
21
28
|
* Get the recorded blob.
|
|
22
29
|
*
|
|
@@ -29,25 +36,95 @@ export default function useMediaRecording({ onDone, } = {}) {
|
|
|
29
36
|
}
|
|
30
37
|
// `start` recording handler
|
|
31
38
|
const start = useCallback((timeslice) => {
|
|
39
|
+
clearData();
|
|
32
40
|
if (!timeslice) {
|
|
33
41
|
return mediaRecordRef?.current?.start();
|
|
34
42
|
}
|
|
35
43
|
if (timeslice < 100) {
|
|
36
44
|
timeslice = 100; // set minimum timeslice to 100ms
|
|
37
45
|
}
|
|
46
|
+
// Record the start time
|
|
47
|
+
recordStartTimestamp.current = Date.now();
|
|
38
48
|
mediaRecordRef?.current?.start(timeslice);
|
|
39
49
|
}, []);
|
|
40
50
|
// `pause` recording handler
|
|
41
51
|
const pause = useCallback(() => {
|
|
52
|
+
isPaused.current = true;
|
|
42
53
|
mediaRecordRef?.current?.pause();
|
|
54
|
+
// Calculate the duration of the recorded audio from the start time
|
|
55
|
+
setDuration(currentDuration => currentDuration + Date.now() - recordStartTimestamp.current);
|
|
43
56
|
}, []);
|
|
44
57
|
// `resume` recording handler
|
|
45
58
|
const resume = useCallback(() => {
|
|
59
|
+
isPaused.current = false;
|
|
46
60
|
mediaRecordRef?.current?.resume();
|
|
61
|
+
// Record the start time
|
|
62
|
+
recordStartTimestamp.current = Date.now();
|
|
47
63
|
}, []);
|
|
48
64
|
// `stop` recording handler
|
|
49
65
|
const stop = useCallback(() => {
|
|
50
66
|
mediaRecordRef?.current?.stop();
|
|
67
|
+
if (state === 'recording') {
|
|
68
|
+
// Calculate the duration of the recorded audio from the start time
|
|
69
|
+
setDuration(currentDuration => currentDuration + Date.now() - recordStartTimestamp.current);
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
72
|
+
// clears the recording state
|
|
73
|
+
const clearData = useCallback(() => {
|
|
74
|
+
recordedChunks.length = 0;
|
|
75
|
+
setBlob(null);
|
|
76
|
+
setError(null);
|
|
77
|
+
setDuration(0);
|
|
78
|
+
isPaused.current = false;
|
|
79
|
+
recordStartTimestamp.current = 0;
|
|
80
|
+
}, []);
|
|
81
|
+
// removes the event listeners
|
|
82
|
+
const clearListeners = useCallback(() => {
|
|
83
|
+
/*
|
|
84
|
+
* mediaRecordRef is not defined when
|
|
85
|
+
* the getUserMedia API is not supported,
|
|
86
|
+
* or when the user has not granted access
|
|
87
|
+
*/
|
|
88
|
+
if (!mediaRecordRef?.current) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
mediaRecordRef.current.removeEventListener('start', onStartListener);
|
|
92
|
+
mediaRecordRef.current.removeEventListener('stop', onStopListener);
|
|
93
|
+
mediaRecordRef.current.removeEventListener('pause', onPauseListener);
|
|
94
|
+
mediaRecordRef.current.removeEventListener('resume', onResumeListener);
|
|
95
|
+
mediaRecordRef.current.removeEventListener('dataavailable', onDataAvailableListener);
|
|
96
|
+
mediaRecordRef.current = null;
|
|
97
|
+
}, []);
|
|
98
|
+
// resets the recording state, initializing the media recorder instance
|
|
99
|
+
const reset = useCallback(() => {
|
|
100
|
+
setState('inactive');
|
|
101
|
+
clearData();
|
|
102
|
+
clearListeners();
|
|
103
|
+
// Check if the getUserMedia API is supported
|
|
104
|
+
if (!navigator.mediaDevices?.getUserMedia) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const constraints = { audio: true };
|
|
108
|
+
navigator.mediaDevices
|
|
109
|
+
.getUserMedia(constraints)
|
|
110
|
+
.then(stream => {
|
|
111
|
+
mediaRecordRef.current = new MediaRecorder(stream);
|
|
112
|
+
mediaRecordRef.current.addEventListener('start', onStartListener);
|
|
113
|
+
mediaRecordRef.current.addEventListener('stop', onStopListener);
|
|
114
|
+
mediaRecordRef.current.addEventListener('pause', onPauseListener);
|
|
115
|
+
mediaRecordRef.current.addEventListener('resume', onResumeListener);
|
|
116
|
+
mediaRecordRef.current.addEventListener('dataavailable', onDataAvailableListener);
|
|
117
|
+
})
|
|
118
|
+
.catch(err => {
|
|
119
|
+
// @todo: handle error
|
|
120
|
+
throw err;
|
|
121
|
+
});
|
|
122
|
+
}, []);
|
|
123
|
+
// stops the recording and sets the error state
|
|
124
|
+
const onError = useCallback((err) => {
|
|
125
|
+
stop();
|
|
126
|
+
setError(typeof err === 'string' ? err : err.message);
|
|
127
|
+
setState('error');
|
|
51
128
|
}, []);
|
|
52
129
|
/**
|
|
53
130
|
* `start` event listener for the media recorder instance.
|
|
@@ -57,12 +134,15 @@ export default function useMediaRecording({ onDone, } = {}) {
|
|
|
57
134
|
}
|
|
58
135
|
/**
|
|
59
136
|
* `stop` event listener for the media recorder instance.
|
|
137
|
+
* Happens after the last `dataavailable` event.
|
|
60
138
|
*
|
|
61
139
|
* @returns {void}
|
|
62
140
|
*/
|
|
63
141
|
function onStopListener() {
|
|
64
|
-
setState('
|
|
65
|
-
|
|
142
|
+
setState('processing');
|
|
143
|
+
const lastBlob = getBlob();
|
|
144
|
+
const url = URL.createObjectURL(lastBlob);
|
|
145
|
+
onDone?.(lastBlob, url);
|
|
66
146
|
// Clear the recorded chunks
|
|
67
147
|
recordedChunks.length = 0;
|
|
68
148
|
}
|
|
@@ -93,51 +173,37 @@ export default function useMediaRecording({ onDone, } = {}) {
|
|
|
93
173
|
recordedChunks.push(data);
|
|
94
174
|
// Create and store the Blob for the recorded chunks
|
|
95
175
|
setBlob(getBlob());
|
|
176
|
+
// If the recorder was paused, it is the last data available event, so we do not update the duration
|
|
177
|
+
if (!isPaused.current) {
|
|
178
|
+
setDuration(currentDuration => {
|
|
179
|
+
const now = Date.now();
|
|
180
|
+
const difference = now - recordStartTimestamp.current;
|
|
181
|
+
// Update the start time
|
|
182
|
+
recordStartTimestamp.current = now;
|
|
183
|
+
return currentDuration + difference;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
96
186
|
}
|
|
97
|
-
//
|
|
187
|
+
// Remove listeners and clear the recorded chunks
|
|
98
188
|
useEffect(() => {
|
|
99
|
-
|
|
100
|
-
if (!navigator.mediaDevices?.getUserMedia) {
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
const constraints = { audio: true };
|
|
104
|
-
navigator.mediaDevices
|
|
105
|
-
.getUserMedia(constraints)
|
|
106
|
-
.then(stream => {
|
|
107
|
-
mediaRecordRef.current = new MediaRecorder(stream);
|
|
108
|
-
mediaRecordRef.current.addEventListener('start', onStartListener);
|
|
109
|
-
mediaRecordRef.current.addEventListener('stop', onStopListener);
|
|
110
|
-
mediaRecordRef.current.addEventListener('pause', onPauseListener);
|
|
111
|
-
mediaRecordRef.current.addEventListener('resume', onResumeListener);
|
|
112
|
-
mediaRecordRef.current.addEventListener('dataavailable', onDataAvailableListener);
|
|
113
|
-
})
|
|
114
|
-
.catch(err => {
|
|
115
|
-
// @todo: handle error
|
|
116
|
-
throw err;
|
|
117
|
-
});
|
|
189
|
+
reset();
|
|
118
190
|
return () => {
|
|
119
|
-
|
|
120
|
-
* mediaRecordRef is not defined when
|
|
121
|
-
* the getUserMedia API is not supported,
|
|
122
|
-
* or when the user has not granted access
|
|
123
|
-
*/
|
|
124
|
-
if (!mediaRecordRef?.current) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
mediaRecordRef.current.removeEventListener('start', onStartListener);
|
|
128
|
-
mediaRecordRef.current.removeEventListener('stop', onStopListener);
|
|
129
|
-
mediaRecordRef.current.removeEventListener('pause', onPauseListener);
|
|
130
|
-
mediaRecordRef.current.removeEventListener('resume', onResumeListener);
|
|
131
|
-
mediaRecordRef.current.removeEventListener('dataavailable', onDataAvailableListener);
|
|
191
|
+
clearListeners();
|
|
132
192
|
};
|
|
133
193
|
}, []);
|
|
134
194
|
return {
|
|
135
195
|
state,
|
|
136
196
|
blob,
|
|
137
197
|
url: blob ? URL.createObjectURL(blob) : null,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
198
|
+
error,
|
|
199
|
+
duration,
|
|
200
|
+
onError,
|
|
201
|
+
controls: {
|
|
202
|
+
start,
|
|
203
|
+
pause,
|
|
204
|
+
resume,
|
|
205
|
+
stop,
|
|
206
|
+
reset,
|
|
207
|
+
},
|
|
142
208
|
};
|
|
143
209
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Post-processing types.
|
|
3
|
+
*/
|
|
4
|
+
export declare const TRANSCRIPTION_POST_PROCESSING_ACTION_SIMPLE_DRAFT = "voice-to-content-simple-draft";
|
|
5
|
+
export type PostProcessingAction = typeof TRANSCRIPTION_POST_PROCESSING_ACTION_SIMPLE_DRAFT;
|
|
6
|
+
/**
|
|
7
|
+
* The return value for the transcription post-processing hook.
|
|
8
|
+
*/
|
|
9
|
+
export type UseTranscriptionPostProcessingReturn = {
|
|
10
|
+
postProcessingResult: string;
|
|
11
|
+
isProcessingTranscription: boolean;
|
|
12
|
+
postProcessingError: string;
|
|
13
|
+
processTranscription: (action: PostProcessingAction, transcription: string) => void;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The props for the transcription post-processing hook.
|
|
17
|
+
*/
|
|
18
|
+
export type UseTranscriptionPostProcessingProps = {
|
|
19
|
+
feature: string;
|
|
20
|
+
onReady?: (postProcessingResult: string) => void;
|
|
21
|
+
onError?: (error: string) => void;
|
|
22
|
+
onUpdate?: (currentPostProcessingResult: string) => void;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* A hook to handle transcription post-processing.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} feature - The feature name that is calling the post-processing actions.
|
|
28
|
+
* @returns {UseTranscriptionPostProcessingReturn} - Object with properties to get the post-processing results.
|
|
29
|
+
*/
|
|
30
|
+
export default function useTranscriptionPostProcessing({ feature, onReady, onError, onUpdate, }: UseTranscriptionPostProcessingProps): UseTranscriptionPostProcessingReturn;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { useCallback, useState } from '@wordpress/element';
|
|
5
|
+
import debugFactory from 'debug';
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import useAiSuggestions from '../use-ai-suggestions/index.js';
|
|
10
|
+
const debug = debugFactory('jetpack-ai-client:use-transcription-post-processing');
|
|
11
|
+
/**
|
|
12
|
+
* Post-processing types.
|
|
13
|
+
*/
|
|
14
|
+
export const TRANSCRIPTION_POST_PROCESSING_ACTION_SIMPLE_DRAFT = 'voice-to-content-simple-draft';
|
|
15
|
+
/**
|
|
16
|
+
* A hook to handle transcription post-processing.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} feature - The feature name that is calling the post-processing actions.
|
|
19
|
+
* @returns {UseTranscriptionPostProcessingReturn} - Object with properties to get the post-processing results.
|
|
20
|
+
*/
|
|
21
|
+
export default function useTranscriptionPostProcessing({ feature, onReady, onError, onUpdate, }) {
|
|
22
|
+
const [postProcessingResult, setPostProcessingResult] = useState('');
|
|
23
|
+
const [postProcessingError, setPostProcessingError] = useState('');
|
|
24
|
+
const [isProcessingTranscription, setIsProcessingTranscription] = useState(false);
|
|
25
|
+
/**
|
|
26
|
+
* Set-up the useAiSuggestions hook.
|
|
27
|
+
*/
|
|
28
|
+
const handleOnSuggestion = useCallback((suggestion) => {
|
|
29
|
+
setPostProcessingResult(suggestion);
|
|
30
|
+
onUpdate?.(suggestion);
|
|
31
|
+
}, [setPostProcessingResult, onUpdate]);
|
|
32
|
+
const handleOnDone = useCallback((result) => {
|
|
33
|
+
setPostProcessingResult(result);
|
|
34
|
+
onUpdate?.(result);
|
|
35
|
+
onReady?.(result);
|
|
36
|
+
}, [setPostProcessingResult, onUpdate, onReady]);
|
|
37
|
+
const handleOnError = useCallback((errorData) => {
|
|
38
|
+
setPostProcessingError(errorData.message);
|
|
39
|
+
onError?.(errorData.message);
|
|
40
|
+
}, [setPostProcessingError, onError]);
|
|
41
|
+
const { request } = useAiSuggestions({
|
|
42
|
+
autoRequest: false,
|
|
43
|
+
onSuggestion: handleOnSuggestion,
|
|
44
|
+
onDone: handleOnDone,
|
|
45
|
+
onError: handleOnError,
|
|
46
|
+
});
|
|
47
|
+
const handleTranscriptionPostProcessing = useCallback((action, transcription) => {
|
|
48
|
+
debug('Post-processing transcription');
|
|
49
|
+
/**
|
|
50
|
+
* Reset the transcription result and error.
|
|
51
|
+
*/
|
|
52
|
+
setPostProcessingResult('');
|
|
53
|
+
setPostProcessingError('');
|
|
54
|
+
setIsProcessingTranscription(true);
|
|
55
|
+
/**
|
|
56
|
+
* Build the prompt to call the suggestion hook.
|
|
57
|
+
*/
|
|
58
|
+
const messages = [
|
|
59
|
+
{
|
|
60
|
+
role: 'jetpack-ai',
|
|
61
|
+
context: {
|
|
62
|
+
type: action,
|
|
63
|
+
content: transcription,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
];
|
|
67
|
+
/**
|
|
68
|
+
* Call the suggestion hook using the message.
|
|
69
|
+
*/
|
|
70
|
+
request(messages, { feature });
|
|
71
|
+
}, [
|
|
72
|
+
setPostProcessingResult,
|
|
73
|
+
setPostProcessingError,
|
|
74
|
+
setIsProcessingTranscription,
|
|
75
|
+
request,
|
|
76
|
+
feature,
|
|
77
|
+
]);
|
|
78
|
+
return {
|
|
79
|
+
postProcessingResult,
|
|
80
|
+
isProcessingTranscription,
|
|
81
|
+
postProcessingError,
|
|
82
|
+
processTranscription: handleTranscriptionPostProcessing,
|
|
83
|
+
};
|
|
84
|
+
}
|
package/build/icons/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { default as aiAssistantIcon } from './ai-assistant';
|
|
2
|
-
export { default as micIcon } from './mic';
|
|
3
|
-
export { default as origamiPlaneIcon } from './origami-plane';
|
|
4
|
-
export { default as playerPlayIcon } from './player-play';
|
|
5
|
-
export { default as playerStopIcon } from './player-stop';
|
|
6
|
-
export { default as playerPauseIcon } from './player-pause';
|
|
7
|
-
export { default as speakToneIcon } from './speak-tone';
|
|
1
|
+
export { default as aiAssistantIcon } from './ai-assistant.js';
|
|
2
|
+
export { default as micIcon } from './mic.js';
|
|
3
|
+
export { default as origamiPlaneIcon } from './origami-plane.js';
|
|
4
|
+
export { default as playerPlayIcon } from './player-play.js';
|
|
5
|
+
export { default as playerStopIcon } from './player-stop.js';
|
|
6
|
+
export { default as playerPauseIcon } from './player-pause.js';
|
|
7
|
+
export { default as speakToneIcon } from './speak-tone.js';
|
package/build/icons/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { default as aiAssistantIcon } from './ai-assistant';
|
|
2
|
-
export { default as micIcon } from './mic';
|
|
3
|
-
export { default as origamiPlaneIcon } from './origami-plane';
|
|
4
|
-
export { default as playerPlayIcon } from './player-play';
|
|
5
|
-
export { default as playerStopIcon } from './player-stop';
|
|
6
|
-
export { default as playerPauseIcon } from './player-pause';
|
|
7
|
-
export { default as speakToneIcon } from './speak-tone';
|
|
1
|
+
export { default as aiAssistantIcon } from './ai-assistant.js';
|
|
2
|
+
export { default as micIcon } from './mic.js';
|
|
3
|
+
export { default as origamiPlaneIcon } from './origami-plane.js';
|
|
4
|
+
export { default as playerPlayIcon } from './player-play.js';
|
|
5
|
+
export { default as playerStopIcon } from './player-stop.js';
|
|
6
|
+
export { default as playerPauseIcon } from './player-pause.js';
|
|
7
|
+
export { default as speakToneIcon } from './speak-tone.js';
|
package/build/icons/mic.js
CHANGED
|
@@ -2,6 +2,6 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
/**
|
|
3
3
|
* External dependencies
|
|
4
4
|
*/
|
|
5
|
-
import { SVG, Rect,
|
|
6
|
-
const mic = (_jsxs(SVG, { width: "24", height: "24", viewBox: "0 0 24 24", xmlns: "http://www.w3.org/2000/SVG", children: [_jsx(
|
|
5
|
+
import { SVG, Rect, Path } from '@wordpress/components';
|
|
6
|
+
const mic = (_jsxs(SVG, { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/SVG", children: [_jsx(Path, { d: "M12 4.75C11.0479 4.75 10.1052 4.93753 9.22554 5.30187C8.34593 5.66622 7.5467 6.20025 6.87348 6.87348C6.20025 7.5467 5.66622 8.34593 5.30187 9.22554C4.93753 10.1052 4.75 11.0479 4.75 12C4.75 12.9521 4.93753 13.8948 5.30187 14.7745C5.66622 15.6541 6.20025 16.4533 6.87348 17.1265C7.5467 17.7997 8.34593 18.3338 9.22554 18.6981C10.1052 19.0625 11.0479 19.25 12 19.25C13.9228 19.25 15.7669 18.4862 17.1265 17.1265C18.4862 15.7669 19.25 13.9228 19.25 12C19.25 10.0772 18.4862 8.23312 17.1265 6.87348C15.7669 5.51384 13.9228 4.75 12 4.75ZM3.25 12C3.25 9.67936 4.17187 7.45376 5.81282 5.81282C7.45376 4.17187 9.67936 3.25 12 3.25C14.3206 3.25 16.5462 4.17187 18.1872 5.81282C19.8281 7.45376 20.75 9.67936 20.75 12C20.75 14.3206 19.8281 16.5462 18.1872 18.1872C16.5462 19.8281 14.3206 20.75 12 20.75C9.67936 20.75 7.45376 19.8281 5.81282 18.1872C4.17187 16.5462 3.25 14.3206 3.25 12Z", fill: "#008710" }), _jsx(Rect, { x: "10.5", y: "8.25", width: "3", height: "4.875", rx: "1.5", fill: "#008710" }), _jsx(Path, { d: "M15 11.25C15 11.644 14.9224 12.0341 14.7716 12.3981C14.6209 12.762 14.3999 13.0927 14.1213 13.3713C13.8427 13.6499 13.512 13.8709 13.1481 14.0216C12.7841 14.1724 12.394 14.25 12 14.25C11.606 14.25 11.2159 14.1724 10.8519 14.0216C10.488 13.8709 10.1573 13.6499 9.87868 13.3713C9.6001 13.0927 9.37913 12.762 9.22836 12.3981C9.0776 12.0341 9 11.644 9 11.25", stroke: "#008710", strokeWidth: "1.125", strokeLinecap: "round" }), _jsx(Path, { d: "M11.4375 15.75C11.4375 16.0607 11.6893 16.3125 12 16.3125C12.3107 16.3125 12.5625 16.0607 12.5625 15.75L11.4375 15.75ZM11.4375 14.25L11.4375 15.75L12.5625 15.75L12.5625 14.25L11.4375 14.25Z", fill: "#008710" })] }));
|
|
7
7
|
export default mic;
|
|
@@ -3,5 +3,5 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
3
3
|
* External dependencies
|
|
4
4
|
*/
|
|
5
5
|
import { SVG, Path, Rect } from '@wordpress/components';
|
|
6
|
-
const playerPause = (_jsxs(SVG, { width: "
|
|
6
|
+
const playerPause = (_jsxs(SVG, { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/SVG", children: [_jsx(Path, { d: "M12 4.75C11.0479 4.75 10.1052 4.93753 9.22554 5.30187C8.34593 5.66622 7.5467 6.20025 6.87348 6.87348C6.20025 7.5467 5.66622 8.34593 5.30187 9.22554C4.93753 10.1052 4.75 11.0479 4.75 12C4.75 12.9521 4.93753 13.8948 5.30187 14.7745C5.66622 15.6541 6.20025 16.4533 6.87348 17.1265C7.5467 17.7997 8.34593 18.3338 9.22554 18.6981C10.1052 19.0625 11.0479 19.25 12 19.25C13.9228 19.25 15.7669 18.4862 17.1265 17.1265C18.4862 15.7669 19.25 13.9228 19.25 12C19.25 10.0772 18.4862 8.23312 17.1265 6.87348C15.7669 5.51384 13.9228 4.75 12 4.75ZM3.25 12C3.25 9.67936 4.17187 7.45376 5.81282 5.81282C7.45376 4.17187 9.67936 3.25 12 3.25C14.3206 3.25 16.5462 4.17187 18.1872 5.81282C19.8281 7.45376 20.75 9.67936 20.75 12C20.75 14.3206 19.8281 16.5462 18.1872 18.1872C16.5462 19.8281 14.3206 20.75 12 20.75C9.67936 20.75 7.45376 19.8281 5.81282 18.1872C4.17187 16.5462 3.25 14.3206 3.25 12Z", fill: "#008710" }), _jsx(Rect, { x: "12.75", y: "9", width: "2.25", height: "6", fill: "#008710" }), _jsx(Rect, { x: "9", y: "9", width: "2.25", height: "6", fill: "#008710" })] }));
|
|
7
7
|
export default playerPause;
|
package/build/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
export { default as requestJwt } from './jwt';
|
|
2
|
-
export { default as SuggestionsEventSource } from './suggestions-event-source';
|
|
3
|
-
export { default as askQuestion } from './ask-question';
|
|
4
|
-
export { default as
|
|
5
|
-
export { default as
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
export
|
|
9
|
-
export * from './
|
|
1
|
+
export { default as requestJwt } from './jwt/index.js';
|
|
2
|
+
export { default as SuggestionsEventSource } from './suggestions-event-source/index.js';
|
|
3
|
+
export { default as askQuestion } from './ask-question/index.js';
|
|
4
|
+
export { default as transcribeAudio } from './audio-transcription/index.js';
|
|
5
|
+
export { default as useAiSuggestions } from './hooks/use-ai-suggestions/index.js';
|
|
6
|
+
export { default as useMediaRecording } from './hooks/use-media-recording/index.js';
|
|
7
|
+
export { default as useAudioTranscription } from './hooks/use-audio-transcription/index.js';
|
|
8
|
+
export { default as useTranscriptionPostProcessing } from './hooks/use-transcription-post-processing/index.js';
|
|
9
|
+
export * from './icons/index.js';
|
|
10
|
+
export * from './components/index.js';
|
|
11
|
+
export * from './data-flow/index.js';
|
|
12
|
+
export * from './types.js';
|
package/build/index.js
CHANGED
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Core library exports
|
|
3
3
|
*/
|
|
4
|
-
export { default as requestJwt } from './jwt';
|
|
5
|
-
export { default as SuggestionsEventSource } from './suggestions-event-source';
|
|
6
|
-
export { default as askQuestion } from './ask-question';
|
|
4
|
+
export { default as requestJwt } from './jwt/index.js';
|
|
5
|
+
export { default as SuggestionsEventSource } from './suggestions-event-source/index.js';
|
|
6
|
+
export { default as askQuestion } from './ask-question/index.js';
|
|
7
|
+
export { default as transcribeAudio } from './audio-transcription/index.js';
|
|
7
8
|
/*
|
|
8
9
|
* Hooks
|
|
9
10
|
*/
|
|
10
|
-
export { default as useAiSuggestions } from './hooks/use-ai-suggestions';
|
|
11
|
-
export { default as useMediaRecording } from './hooks/use-media-recording';
|
|
11
|
+
export { default as useAiSuggestions } from './hooks/use-ai-suggestions/index.js';
|
|
12
|
+
export { default as useMediaRecording } from './hooks/use-media-recording/index.js';
|
|
13
|
+
export { default as useAudioTranscription } from './hooks/use-audio-transcription/index.js';
|
|
14
|
+
export { default as useTranscriptionPostProcessing } from './hooks/use-transcription-post-processing/index.js';
|
|
12
15
|
/*
|
|
13
16
|
* Components: Icons
|
|
14
17
|
*/
|
|
15
|
-
export * from './icons';
|
|
18
|
+
export * from './icons/index.js';
|
|
16
19
|
/*
|
|
17
20
|
* Components
|
|
18
21
|
*/
|
|
19
|
-
export * from './components';
|
|
22
|
+
export * from './components/index.js';
|
|
20
23
|
/*
|
|
21
24
|
* Contexts
|
|
22
25
|
*/
|
|
23
|
-
export * from './data-flow';
|
|
26
|
+
export * from './data-flow/index.js';
|
|
24
27
|
/*
|
|
25
28
|
* Types
|
|
26
29
|
*/
|
|
27
|
-
export * from './types';
|
|
30
|
+
export * from './types.js';
|
package/build/jwt/index.js
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { isSimpleSite } from '@automattic/jetpack-shared-extension-utils';
|
|
5
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
6
5
|
import debugFactory from 'debug';
|
|
6
|
+
/**
|
|
7
|
+
* Internal dependencies
|
|
8
|
+
*/
|
|
9
|
+
import apiFetch from '../api-fetch/index.js';
|
|
7
10
|
const debug = debugFactory('jetpack-ai-client:jwt');
|
|
8
11
|
const JWT_TOKEN_ID = 'jetpack-ai-jwt';
|
|
9
12
|
const JWT_TOKEN_EXPIRATION_TIME = 2 * 60 * 1000; // 2 minutes
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
import { EventSourceMessage } from '@microsoft/fetch-event-source';
|
|
5
|
-
import type { AiModelTypeProp, PromptProp } from '../types';
|
|
5
|
+
import type { AiModelTypeProp, PromptProp } from '../types.js';
|
|
6
6
|
type SuggestionsEventSourceConstructorArgs = {
|
|
7
7
|
url?: string;
|
|
8
8
|
question: PromptProp;
|
|
@@ -6,12 +6,12 @@ import debugFactory from 'debug';
|
|
|
6
6
|
/**
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
|
-
import { getErrorData } from '../hooks/use-ai-suggestions';
|
|
10
|
-
import requestJwt from '../jwt';
|
|
9
|
+
import { getErrorData } from '../hooks/use-ai-suggestions/index.js';
|
|
10
|
+
import requestJwt from '../jwt/index.js';
|
|
11
11
|
/*
|
|
12
12
|
* Types & constants
|
|
13
13
|
*/
|
|
14
|
-
import { ERROR_CONTEXT_TOO_LARGE, ERROR_MODERATION, ERROR_NETWORK, ERROR_QUOTA_EXCEEDED, ERROR_RESPONSE, ERROR_SERVICE_UNAVAILABLE, ERROR_UNCLEAR_PROMPT, } from '../types';
|
|
14
|
+
import { ERROR_CONTEXT_TOO_LARGE, ERROR_MODERATION, ERROR_NETWORK, ERROR_QUOTA_EXCEEDED, ERROR_RESPONSE, ERROR_SERVICE_UNAVAILABLE, ERROR_UNCLEAR_PROMPT, } from '../types.js';
|
|
15
15
|
const debug = debugFactory('jetpack-ai-client:suggestions-event-source');
|
|
16
16
|
/**
|
|
17
17
|
* SuggestionsEventSource is a wrapper around EvenTarget that emits
|
package/build/types.d.ts
CHANGED
|
@@ -13,8 +13,11 @@ export type PromptItemProps = {
|
|
|
13
13
|
};
|
|
14
14
|
export type PromptMessagesProp = Array<PromptItemProps>;
|
|
15
15
|
export type PromptProp = PromptMessagesProp | string;
|
|
16
|
-
export type { UseAiContextOptions } from './data-flow/use-ai-context';
|
|
17
|
-
export type { RequestingErrorProps } from './hooks/use-ai-suggestions';
|
|
16
|
+
export type { UseAiContextOptions } from './data-flow/use-ai-context.js';
|
|
17
|
+
export type { RequestingErrorProps } from './hooks/use-ai-suggestions/index.js';
|
|
18
|
+
export type { UseAudioTranscriptionProps, UseAudioTranscriptionReturn, } from './hooks/use-audio-transcription/index.js';
|
|
19
|
+
export type { UseTranscriptionPostProcessingProps, UseTranscriptionPostProcessingReturn, PostProcessingAction, } from './hooks/use-transcription-post-processing/index.js';
|
|
20
|
+
export { TRANSCRIPTION_POST_PROCESSING_ACTION_SIMPLE_DRAFT } from './hooks/use-transcription-post-processing/index.js';
|
|
18
21
|
export declare const REQUESTING_STATES: readonly ["init", "requesting", "suggesting", "done", "error"];
|
|
19
22
|
export type RequestingStateProp = (typeof REQUESTING_STATES)[number];
|
|
20
23
|
export declare const AI_MODEL_GPT_3_5_Turbo_16K: "gpt-3.5-turbo-16k";
|
package/build/types.js
CHANGED
|
@@ -5,6 +5,10 @@ export const ERROR_CONTEXT_TOO_LARGE = 'error_context_too_large';
|
|
|
5
5
|
export const ERROR_NETWORK = 'error_network';
|
|
6
6
|
export const ERROR_UNCLEAR_PROMPT = 'error_unclear_prompt';
|
|
7
7
|
export const ERROR_RESPONSE = 'error_response';
|
|
8
|
+
/*
|
|
9
|
+
* Hook constants
|
|
10
|
+
*/
|
|
11
|
+
export { TRANSCRIPTION_POST_PROCESSING_ACTION_SIMPLE_DRAFT } from './hooks/use-transcription-post-processing/index.js';
|
|
8
12
|
/*
|
|
9
13
|
* Requests types
|
|
10
14
|
*/
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"private": false,
|
|
3
3
|
"name": "@automattic/jetpack-ai-client",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.7.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": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"types": "./build/index.d.ts",
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@automattic/jetpack-base-styles": "^0.6.17",
|
|
42
|
-
"@automattic/jetpack-connection": "^0.32.
|
|
42
|
+
"@automattic/jetpack-connection": "^0.32.2",
|
|
43
43
|
"@automattic/jetpack-shared-extension-utils": "^0.14.2",
|
|
44
44
|
"@microsoft/fetch-event-source": "2.0.1",
|
|
45
45
|
"@types/react": "18.2.33",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import apiFetchMod from '@wordpress/api-fetch';
|
|
5
|
+
|
|
6
|
+
// @wordpress/api-fetch (as of 6.47.0) declares itself in such a way that tsc and node see the function at apiFetchMod.default
|
|
7
|
+
// while some other environments (including code running inside WordPress itself) see it at apiFetch.
|
|
8
|
+
// See https://arethetypeswrong.github.io/?p=@wordpress/api-fetch@6.47.0
|
|
9
|
+
// This is a helper to simplify the usage of the api-fetch module on the ai-client package.
|
|
10
|
+
type ApiFetchType = typeof apiFetchMod.default;
|
|
11
|
+
const apiFetch: ApiFetchType = ( apiFetchMod.default ?? apiFetchMod ) as ApiFetchType;
|
|
12
|
+
|
|
13
|
+
export default apiFetch;
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
4
|
import debugFactory from 'debug';
|
|
5
|
-
import SuggestionsEventSource from '../suggestions-event-source';
|
|
5
|
+
import SuggestionsEventSource from '../suggestions-event-source/index.js';
|
|
6
6
|
/*
|
|
7
7
|
* Types & constants
|
|
8
8
|
*/
|
|
9
|
-
import type { AiModelTypeProp, PromptProp } from '../types';
|
|
9
|
+
import type { AiModelTypeProp, PromptProp } from '../types.js';
|
|
10
10
|
|
|
11
11
|
export type AskQuestionOptionsArgProps = {
|
|
12
12
|
/*
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import debugFactory from 'debug';
|
|
5
|
+
/**
|
|
6
|
+
* Internal dependencies
|
|
7
|
+
*/
|
|
8
|
+
import apiFetch from '../api-fetch/index.js';
|
|
9
|
+
import requestJwt from '../jwt/index.js';
|
|
10
|
+
|
|
11
|
+
const debug = debugFactory( 'jetpack-ai-client:audio-transcription' );
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The response from the audio transcription service.
|
|
15
|
+
*/
|
|
16
|
+
type AudioTranscriptionResponse = {
|
|
17
|
+
/**
|
|
18
|
+
* The transcribed text.
|
|
19
|
+
*/
|
|
20
|
+
text: string;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A function that takes an audio blob and transcribes it.
|
|
25
|
+
*
|
|
26
|
+
* @param {Blob} audio - The audio to be transcribed, from a recording or from a file.
|
|
27
|
+
* @param {string} feature - The feature name that is calling the transcription.
|
|
28
|
+
* @returns {Promise<string>} - The promise of a string containing the transcribed audio.
|
|
29
|
+
*/
|
|
30
|
+
export default async function transcribeAudio( audio: Blob, feature?: string ): Promise< string > {
|
|
31
|
+
debug( 'Transcribing audio: %o. Feature: %o', audio, feature );
|
|
32
|
+
|
|
33
|
+
// Get a token to use the transcription service
|
|
34
|
+
let token = '';
|
|
35
|
+
try {
|
|
36
|
+
token = ( await requestJwt() ).token;
|
|
37
|
+
} catch ( error ) {
|
|
38
|
+
debug( 'Error getting token: %o', error );
|
|
39
|
+
return Promise.reject( error );
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Build a FormData object to hold the audio file
|
|
43
|
+
const formData = new FormData();
|
|
44
|
+
formData.append( 'audio_file', audio );
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const headers = {
|
|
48
|
+
Authorization: `Bearer ${ token }`,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const response: AudioTranscriptionResponse = await apiFetch( {
|
|
52
|
+
url: `https://public-api.wordpress.com/wpcom/v2/jetpack-ai-transcription${
|
|
53
|
+
feature ? `?feature=${ feature }` : ''
|
|
54
|
+
}`,
|
|
55
|
+
method: 'POST',
|
|
56
|
+
body: formData,
|
|
57
|
+
headers,
|
|
58
|
+
} );
|
|
59
|
+
|
|
60
|
+
debug( 'Transcription response: %o', response );
|
|
61
|
+
|
|
62
|
+
return response.text;
|
|
63
|
+
} catch ( error ) {
|
|
64
|
+
debug( 'Transcription error response: %o', error );
|
|
65
|
+
return Promise.reject( error );
|
|
66
|
+
}
|
|
67
|
+
}
|