1
- import { streamText } from 'ai'
2
- import axios from 'axios'
3
1
import { Controller , Session , Req , Res , Post , Get } from 'routing-controllers'
4
2
import { Request , Response } from 'express'
3
+ import { pipeline } from "node:stream/promises"
5
4
import { Service } from 'typedi'
6
5
import OBPClientService from '../services/OBPClientService'
7
6
import OpeyClientService from '../services/OpeyClientService'
8
- import { v6 as uuid6 } from 'uuid' ;
9
- import { Transform } from 'stream'
7
+
10
8
import { UserInput } from '../schema/OpeySchema'
11
9
12
10
@Service ( )
@@ -41,7 +39,7 @@ export class OpeyController {
41
39
async streamOpey (
42
40
@Session ( ) session : any ,
43
41
@Req ( ) request : Request ,
44
- @Res ( ) response : Response
42
+ @Res ( ) response : Response ,
45
43
) {
46
44
47
45
let user_input : UserInput
@@ -59,66 +57,46 @@ export class OpeyController {
59
57
60
58
console . log ( "Calling OpeyClientService.stream" )
61
59
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);
66
64
67
- callback ( ) ;
68
- }
69
- } )
65
+ // callback();
66
+ // }
67
+ // })
70
68
71
- let stream : ReadableStream | null = null
69
+ let stream : NodeJS . ReadableStream | null = null
72
70
73
71
try {
74
72
// Read stream from OpeyClientService
75
73
stream = await this . opeyClientService . stream ( user_input )
76
- console . debug ( `Stream received readable: ${ stream } ` )
74
+ console . debug ( `Stream received readable: ${ stream ?. readable } ` )
77
75
78
76
} catch ( error ) {
79
77
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' } )
82
79
}
83
80
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' } )
88
84
}
89
85
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
+ } )
102
96
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
+ } )
117
98
118
- } catch ( error ) {
119
- console . error ( "Error writing data: " , error )
120
- response . status ( 500 ) . json ( { error : 'Internal Server Error' } )
121
- }
99
+
122
100
}
123
101
124
102
@Post ( '/invoke' )
0 commit comments