1- import { streamText } from 'ai'
2- import axios from 'axios'
31import { Controller , Session , Req , Res , Post , Get } from 'routing-controllers'
42import { Request , Response } from 'express'
3+ import { pipeline } from "node:stream/promises"
54import { Service } from 'typedi'
65import OBPClientService from '../services/OBPClientService'
76import OpeyClientService from '../services/OpeyClientService'
8- import { v6 as uuid6 } from 'uuid' ;
9- import { Transform } from 'stream'
7+
108import { UserInput } from '../schema/OpeySchema'
119
1210@Service ( )
@@ -41,7 +39,7 @@ export class OpeyController {
4139 async streamOpey (
4240 @Session ( ) session : any ,
4341 @Req ( ) request : Request ,
44- @Res ( ) response : Response
42+ @Res ( ) response : Response ,
4543 ) {
4644
4745 let user_input : UserInput
@@ -59,66 +57,46 @@ export class OpeyController {
5957
6058 console . log ( "Calling OpeyClientService.stream" )
6159
62- const streamMiddlewareTransform = new Transform ( {
63- transform ( chunk , encoding , callback ) {
64- console . log ( `Logged Chunk: ${ chunk } ` )
65- this . push ( chunk ) ;
60+ // const streamMiddlewareTransform = new Transform({
61+ // transform(chunk, encoding, callback) {
62+ // console.log(`Logged Chunk: ${chunk}`)
63+ // this.push(chunk);
6664
67- callback ( ) ;
68- }
69- } )
65+ // callback();
66+ // }
67+ // })
7068
71- let stream : ReadableStream | null = null
69+ let stream : NodeJS . ReadableStream | null = null
7270
7371 try {
7472 // Read stream from OpeyClientService
7573 stream = await this . opeyClientService . stream ( user_input )
76- console . debug ( `Stream received readable: ${ stream } ` )
74+ console . debug ( `Stream received readable: ${ stream ?. readable } ` )
7775
7876 } catch ( error ) {
7977 console . error ( "Error reading stream: " , error )
80- response . status ( 500 ) . json ( { error : 'Internal Server Error' } )
81- return
78+ return response . status ( 500 ) . json ( { error : 'Internal Server Error' } )
8279 }
8380
84- if ( ! stream ) {
85- console . error ( "Stream is not readable" )
86- response . status ( 500 ) . json ( { error : 'Internal Server Error' } )
87- return
81+ if ( ! stream || ! stream . readable ) {
82+ console . error ( "Stream is not recieved or not readable" )
83+ return response . status ( 500 ) . json ( { error : 'Internal Server Error' } )
8884 }
8985
90- try {
91- // response.writeHead(200, {
92- // 'Content-Type': "text/event-stream",
93- // 'Cache-Control': "no-cache",
94- // 'Connection': "keep-alive"
95- // });
96-
97- response . setHeader ( 'Content-Type' , 'text/event-stream' )
98- response . setHeader ( 'Cache-Control' , 'no-cache' )
99- response . setHeader ( 'Connection' , 'keep-alive' )
100-
101- let data : any [ ] = [ ]
86+ return new Promise < Response > ( ( resolve , reject ) => {
87+ stream . pipe ( response )
88+ stream . on ( 'end' , ( ) => {
89+ response . status ( 200 )
90+ resolve ( response )
91+ } )
92+ stream . on ( 'error' , ( error ) => {
93+ console . error ( "Error piping stream: " , error )
94+ reject ( error )
95+ } )
10296
103- const streamReader = stream . getReader ( )
104- console . log ( "Got stream reader: " , streamReader )
105-
106- streamReader
107- . read ( )
108- . then ( function processText ( { done, value } ) {
109- if ( done ) {
110- console . log ( "Stream done" )
111- return response . status ( 200 ) . json ( data )
112- }
113- console . log ( "Stream value: " , value )
114- data . push ( value )
115- response . write ( `data: ${ value } \n\n` )
116- } )
97+ } )
11798
118- } catch ( error ) {
119- console . error ( "Error writing data: " , error )
120- response . status ( 500 ) . json ( { error : 'Internal Server Error' } )
121- }
99+
122100 }
123101
124102 @Post ( '/invoke' )
0 commit comments