@@ -5,34 +5,38 @@ import WebSocket, { WebSocketServer } from "ws";
55import VoiceResponse from "twilio/lib/twiml/VoiceResponse" ;
66import Restack from "@restackio/restack-sdk-ts" ;
77import { twilioStreamWorkflow } from "./workflows/twilioStream" ;
8- // import cors from "cors";
8+ import cors from "cors" ;
99
1010const app = express ( ) ;
1111const server = createServer ( app ) ;
1212const wss = new WebSocketServer ( { server } ) ;
1313const PORT = process . env . PORT || 4000 ;
1414export const websocketAddress = `wss://${ process . env . SERVER } /connection` ;
1515
16- // app.use(cors());
16+ app . use ( cors ( ) ) ;
1717app . use ( express . json ( ) ) ;
1818
1919app . post ( "/start" , async ( req , res ) => {
20- const { streamSid } = req . body ;
21-
22- if ( ! streamSid ) {
23- return res . status ( 400 ) . send ( { error : "streamSid is required" } ) ;
24- }
25-
2620 try {
2721 const restack = new Restack ( ) ;
2822
23+ const workflowId = `${ Date . now ( ) } -${ twilioStreamWorkflow . name } ` ;
24+
2925 const workflowRunId = await restack . schedule ( {
30- workflowName : "twilioAgentWorkflow" ,
31- workflowId : `${ Date . now ( ) } -twilioAgentWorkflow` ,
32- input : { streamSid } ,
26+ workflowName : twilioStreamWorkflow . name ,
27+ workflowId,
3328 } ) ;
3429
35- res . status ( 200 ) . send ( { workflowRunId, websocketAddress } ) ;
30+ if ( workflowRunId ) {
31+ await restack . update ( {
32+ workflowId,
33+ runId : workflowRunId ,
34+ updateName : "streamInfo" ,
35+ input : { streamSid : workflowRunId } ,
36+ } ) ;
37+
38+ res . status ( 200 ) . send ( { streamSid : workflowRunId , websocketAddress } ) ;
39+ }
3640 } catch ( error ) {
3741 console . error ( "Error scheduling workflow:" , error ) ;
3842 res . status ( 500 ) . send ( { error : "Failed to schedule workflow" } ) ;
0 commit comments