@@ -36,6 +36,7 @@ import {
3636 processNDJSONStream ,
3737 REALTIME_AGENTS_SERVICE ,
3838 RealtimeKitTransport ,
39+ TextProcessor ,
3940 type RealtimePipelineComponent
4041} from "./realtime-agent" ;
4142import { randomUUID } from "node:crypto" ;
@@ -356,10 +357,11 @@ export class Agent<
356357 return DataKind . Text ;
357358 }
358359
359- schema ( ) {
360+ schema ( ) : { name : string ; type : string ; [ K : string ] : any } {
360361 return {
361362 name : this . _ParentClass . name ,
362- type : "agent"
363+ type : "agent" ,
364+ internal_sdk : true
363365 } ;
364366 }
365367
@@ -1781,25 +1783,17 @@ export class Agent<
17811783 const { request } = getCurrentAgent ( ) ;
17821784 if ( ! request ) throw new Error ( "request is required" ) ;
17831785
1784- console . log ( "initializing realtime pipeline" ) ;
17851786 const requestUrl = new URL ( request . url ) ;
17861787 const agentId = this . name ;
17871788 const agentName = camelCaseToKebabCase ( this . _ParentClass . name ) ;
17881789 const agentURL = `${ requestUrl . host } /agents/${ agentName } /${ agentId } /realtime` ;
17891790
1790- const elements : { name : string ; [ K : string ] : any } [ ] = [
1791- {
1792- name : "ws_out" ,
1793- type : "websocket_out" ,
1794- internal_sdk : true ,
1795- url : `wss://${ agentURL } /ws`
1796- } ,
1797- {
1798- name : "ws_in_text" ,
1799- internal_sdk : true ,
1800- type : "websocket_in"
1801- }
1802- ] ;
1791+ // We need to check if the components array have any instance of
1792+ // Agent or TextProcessor, in that case we need to split the components
1793+ // into two layers, where the first layers end at that component
1794+ // and the second layer starts from that component. The Agent/TextProcessor
1795+ // are websocket element, we will be using bidirectional websocket so only
1796+ // one websocket element will be acting as input and output both.
18031797
18041798 const layers : { id : number ; name : string ; elements : string [ ] } [ ] = [
18051799 {
@@ -1808,32 +1802,41 @@ export class Agent<
18081802 elements : [ ]
18091803 }
18101804 ] ;
1805+ let elements : { name : string ; [ K : string ] : any } [ ] = [ ] ;
18111806
18121807 for ( const component of components ) {
1813- if ( component instanceof Agent ) {
1814- layers [ layers . length - 1 ] . elements . push ( "ws_out" ) ;
1808+ let schema = component . schema ( ) ;
1809+ if ( component instanceof Agent || component instanceof TextProcessor ) {
1810+ if ( component instanceof Agent ) {
1811+ schema . type = "websocket" ;
1812+ schema . url = `wss://${ agentURL } ` ;
1813+ }
1814+ layers [ layers . length - 1 ] . elements . push ( schema . name ) ;
1815+
18151816 layers . push ( {
18161817 id : layers . length + 1 ,
18171818 name : `default-${ layers . length + 1 } ` ,
1818- elements : [ "ws_in_text" ]
1819+ elements : [ ]
18191820 } ) ;
1820- } else {
1821- if ( elements . filter ( ( e ) => e . name === component . name ) . length === 0 ) {
1822- if ( component instanceof RealtimeKitTransport )
1823- elements . push ( {
1824- ...component . schema ( ) ,
1825- worker_url : `https://${ agentURL } `
1826- } ) ;
1827- else elements . push ( component . schema ( ) ) ;
1828- }
1829- layers [ layers . length - 1 ] . elements . push ( component . name ) ;
18301821 }
1822+
1823+ if ( component instanceof RealtimeKitTransport ) {
1824+ schema . worker_url = `https://${ agentURL } ` ;
1825+ }
1826+ elements . push ( schema ) ;
1827+ layers [ layers . length - 1 ] . elements . push ( schema . name ) ;
18311828 }
18321829
1830+ elements = elements . filter (
1831+ ( v , idx , arr ) => idx === arr . findIndex ( ( v1 ) => v1 . name === v . name )
1832+ ) ;
1833+
1834+ console . log ( "layers" , layers , "elements" , elements ) ;
1835+
18331836 const response = await fetch (
18341837 `${ CLOUDFLARE_BASE } /client/v4/accounts/${ CF_ACCOUNT_ID } /realtime/agents/pipeline` ,
1838+ // `${REALTIME_AGENTS_SERVICE}/pipeline`,
18351839 {
1836- // const response = await fetch(`${REALTIME_AGENTS_SERVICE}/pipeline`, {
18371840 method : "POST" ,
18381841 headers : {
18391842 Authorization : `Bearer ${ CF_API_TOKEN } ` ,
@@ -1878,8 +1881,7 @@ export class Agent<
18781881 // check if instance is already started
18791882 if ( this . realtimePipelineRunning )
18801883 throw new Error ( "agent is already running" ) ;
1881- const components = this . realtimePipelineComponents ! ( ) ;
1882- await this . initRealtimePipeline ( components ) ;
1884+ await this . initRealtimePipeline ( this . realtimePipelineComponents ! ( ) ) ;
18831885
18841886 const startResponse = await fetch (
18851887 `${ REALTIME_AGENTS_SERVICE } /pipeline?authToken=${ this . _rtk_authToken } ` ,
0 commit comments