@@ -13,6 +13,7 @@ export class SynthesisHandle {
13
13
static readonly FLUSH_SENTINEL = Symbol ( 'FLUSH_SENTINEL' ) ;
14
14
15
15
#speechId: string ;
16
+ text ?: string ;
16
17
ttsSource : SpeechSource ;
17
18
#agentPlayout: AgentPlayout ;
18
19
tts : TTS ;
@@ -97,7 +98,7 @@ export class AgentOutput {
97
98
// eslint-disable-next-line @typescript-eslint/no-unused-vars
98
99
return new CancellablePromise ( async ( resolve , _ , onCancel ) => {
99
100
const ttsSource = await handle . ttsSource ;
100
- let task : CancellablePromise < void > ;
101
+ let task : CancellablePromise < string > ;
101
102
if ( typeof ttsSource === 'string' ) {
102
103
task = stringSynthesisTask ( ttsSource , handle ) ;
103
104
} else {
@@ -113,6 +114,10 @@ export class AgentOutput {
113
114
} finally {
114
115
if ( handle . intFut . done ) {
115
116
gracefullyCancel ( task ) ;
117
+ } else {
118
+ task . then ( ( text ) => {
119
+ handle . text = text ;
120
+ } ) ;
116
121
}
117
122
}
118
123
@@ -121,9 +126,9 @@ export class AgentOutput {
121
126
}
122
127
}
123
128
124
- const stringSynthesisTask = ( text : string , handle : SynthesisHandle ) : CancellablePromise < void > => {
129
+ const stringSynthesisTask = ( text : string , handle : SynthesisHandle ) : CancellablePromise < string > => {
125
130
// eslint-disable-next-line @typescript-eslint/no-unused-vars
126
- return new CancellablePromise < void > ( async ( resolve , _ , onCancel ) => {
131
+ return new CancellablePromise ( async ( resolve , _ , onCancel ) => {
127
132
let cancelled = false ;
128
133
onCancel ( ( ) => {
129
134
cancelled = true ;
@@ -141,16 +146,17 @@ const stringSynthesisTask = (text: string, handle: SynthesisHandle): Cancellable
141
146
}
142
147
handle . queue . put ( SynthesisHandle . FLUSH_SENTINEL ) ;
143
148
144
- resolve ( ) ;
149
+ resolve ( text ) ;
145
150
} ) ;
146
151
} ;
147
152
148
153
const streamSynthesisTask = (
149
154
stream : AsyncIterable < string > ,
150
155
handle : SynthesisHandle ,
151
- ) : CancellablePromise < void > => {
156
+ ) : CancellablePromise < string > => {
152
157
// eslint-disable-next-line @typescript-eslint/no-unused-vars
153
- return new CancellablePromise < void > ( async ( resolve , _ , onCancel ) => {
158
+ return new CancellablePromise ( async ( resolve , _ , onCancel ) => {
159
+ let fullText = '' ;
154
160
let cancelled = false ;
155
161
onCancel ( ( ) => {
156
162
cancelled = true ;
@@ -170,12 +176,13 @@ const streamSynthesisTask = (
170
176
readGeneratedAudio ( ) ;
171
177
172
178
for await ( const text of stream ) {
179
+ fullText += text ;
173
180
if ( cancelled ) break ;
174
181
ttsStream . pushText ( text ) ;
175
182
}
176
183
ttsStream . flush ( ) ;
177
184
ttsStream . endInput ( ) ;
178
185
179
- resolve ( ) ;
186
+ resolve ( fullText ) ;
180
187
} ) ;
181
188
} ;
0 commit comments