@@ -147,54 +147,13 @@ if (window.MONGOOSE_STUDIO_CONFIG.isLambda) {
147147 updateDocument : function updateDocument ( params ) {
148148 return client . post ( '' , { action : 'Model.updateDocument' , ...params } ) . then ( res => res . data ) ;
149149 } ,
150+ createChatMessage ( params ) {
151+ return client . post ( '' , { action : 'Model.createChatMessage' , ...params } ) . then ( res => res . data ) ;
152+ } ,
150153 streamChatMessage : async function * streamChatMessage ( params ) {
151- const accessToken = window . localStorage . getItem ( '_mongooseStudioAccessToken' ) || null ;
152- const url = window . MONGOOSE_STUDIO_CONFIG . baseURL + '?' + new URLSearchParams ( { action : 'Model.streamChatMessage' , ...params } ) . toString ( ) ;
153-
154- const response = await fetch ( url , {
155- method : 'GET' ,
156- headers : {
157- Authorization : `${ accessToken } ` ,
158- Accept : 'text/event-stream'
159- }
160- } ) ;
161-
162- if ( ! response . ok ) {
163- throw new Error ( `HTTP error! Status: ${ response . status } ` ) ;
164- }
165-
166- const reader = response . body . getReader ( ) ;
167- const decoder = new TextDecoder ( 'utf-8' ) ;
168- let buffer = '' ;
169-
170- while ( true ) {
171- const { done, value } = await reader . read ( ) ;
172- if ( done ) break ;
173- buffer += decoder . decode ( value , { stream : true } ) ;
174-
175- let eventEnd ;
176- while ( ( eventEnd = buffer . indexOf ( '\n\n' ) ) !== - 1 ) {
177- const eventStr = buffer . slice ( 0 , eventEnd ) ;
178- buffer = buffer . slice ( eventEnd + 2 ) ;
179-
180- // Parse SSE event
181- const lines = eventStr . split ( '\n' ) ;
182- let data = '' ;
183- for ( const line of lines ) {
184- if ( line . startsWith ( 'data:' ) ) {
185- data += line . slice ( 5 ) . trim ( ) ;
186- }
187- }
188- if ( data ) {
189- try {
190- const res = JSON . parse ( data ) ;
191- yield res ;
192- } catch ( err ) {
193- yield data ;
194- }
195- }
196- }
197- }
154+ // Don't stream on Next.js or Netlify for now.
155+ const data = await client . post ( '' , { action : 'Model.createChatMessage' , ...params } ) . then ( res => res . data ) ;
156+ yield { textPart : data . text } ;
198157 } ,
199158 updateDocuments : function updateDocuments ( params ) {
200159 return client . post ( '' , { action : 'Model.updateDocuments' , ...params } ) . then ( res => res . data ) ;
@@ -299,6 +258,9 @@ if (window.MONGOOSE_STUDIO_CONFIG.isLambda) {
299258 createChart : function ( params ) {
300259 return client . post ( '/Model/createChart' , params ) . then ( res => res . data ) ;
301260 } ,
261+ createChatMessage : function ( params ) {
262+ return client . post ( '/Model/createChatMessage' , params ) . then ( res => res . data ) ;
263+ } ,
302264 createDocument : function ( params ) {
303265 return client . post ( '/Model/createDocument' , params ) . then ( res => res . data ) ;
304266 } ,
0 commit comments