33    import  ' drawflow/dist/drawflow.min.css'  ; 
44    import  ' $lib/drawflow/drawflow.css'  ; 
55    import  { onMount , createEventDispatcher  } from  ' svelte'  ; 
6- 
6+     import  { replaceNewLine  } from  ' $lib/helpers/http'  ; 
7+     import  { newConversation  } from  ' $lib/services/conversation-service'  ; 
8+     import  { conversationStore , getConversationStore  } from  ' $lib/helpers/store.js'  ; 
9+      
710    /**  @type  {import('$types').AgentModel}  */  
811    export  let  agent; 
912    /**  @type  {import('$types').AgentTemplate[]}  */  
1013    let  taskNodes =  []; 
14+     /**  @type  {DrawflowNode}  */  
15+     let  selectedNode; 
1116
1217    const  dispatch  =  createEventDispatcher (); 
1318     
2732            editor .on (' nodeSelected'  , function (id ) { 
2833                console .log (" Node selected "   +  id); 
2934                //  emit event 
35+                 selectedNode =  editor .getNodeFromId (id); 
3036            }); 
3137            renderTaskFlow (editor); 
3238        } 
3743        let  posX =  0 ; 
3844        const  nodeSpaceX  =  300 , nodeSpaceY =  120 ; 
3945
40-         let  posY =  nodeSpaceY *  (agent .templates .length  +  1 ) /  2 ; 
46+         const  templates  =  agent .templates .filter (t  =>  t .name .startsWith (" task."  )); 
47+         let  posY =  nodeSpaceY *  (templates .length  +  1 ) /  2 ; 
4148
42-         //  add end-user node 
49+         //  add agent node 
50+         let  agentNodeHtml =  agent .icon_url  ?  ` <img src=${ agent .icon_url }   height="30" />`   :  " "  ; 
51+         agentNodeHtml +=  ` <span class="h6 ms-2">${ agent .name }  </span>`  ; 
4352        let  agentNodeId =  editor .addNode (' agent'  , 0 , 1 , posX, posY, ' agent'  ,  
4453        { 
45-             id:  " "  
46-         }, ` <img src=${ agent .icon_url }   height="30" /><span class="h6">${ agent .name }  </span>`  , false ); 
54+             is_agent:  true , 
55+             agent:  agent .name  
56+         }, agentNodeHtml, false ); 
4757
4858        posY =  100 ; 
4959        posX +=  nodeSpaceX; 
50-         agent .templates .forEach (template  =>  {        
51-             const  chatTestLinkHtml  =  ` <a href= "/page/agent/${ agent .id }  /task/${ template .name }  " class="btn btn-primary float-end" target="_blank"><i class="bx bx-run"></i></a>`  ; 
52-             let  html =  ` <span class="h6">${ template .name }  </span>${ chatTestLinkHtml} `  ; 
60+ 
61+         //  render tasks 
62+         templates .forEach (template  =>  { 
63+             const  actionLink  =  ` /page/agent/${ agent .id }  /task/${ template .name } `  ; 
64+             let  html =  ` <span class="h6">${ template .name }  </span>`  ; 
65+             html +=  ` <hr/><div style="max-height: 50px; overflow: hidden;">${ replaceNewLine (template .content )}  </div>`  ; 
5366             
5467            const  data  =  { 
55-                 id :  agent .id , 
56-                 agent :   agent .name  
68+                 agent :  agent .name , 
69+                 task :   template .name ,  
5770            }; 
5871            let  nid =  editor .addNode (' agent'  , 1 , 0 , posX, posY, ' enabled-node'  , data, html, false ); 
5972            editor .addConnection (agentNodeId, nid, " output_1"  , " input_1"  ); 
6275        }); 
6376    } 
6477     
65-     /**  @param  {import('$types').AgentModel}  router  */  
66-     function  getPlannerName (router ) { 
67-         const  planner  =  router .routing_rules .find (p  =>  p .type  ==  " planner"  ); 
78+     /**  @param  {import('$types').AgentModel}  agent  */  
79+     function  getPlannerName (agent ) { 
80+         const  planner  =  agent .routing_rules .find (p  =>  p .type  ==  " planner"  ); 
6881        return  planner? .field  ??  " NaviePlanner"  ; 
6982    } 
83+ 
84+     async  function  handleTestInChat () { 
85+         window .location .href  =  ` /chat/${ agent .id } `  ; 
86+     } 
87+ 
88+     async  function  handleRunTask () { 
89+         const  conversation  =  await  newConversation (agent .id ); 
90+         conversationStore .set (conversation); 
91+     } 
7092< / script> 
7193
94+ < div> 
95+     {#if  selectedNode &&  selectedNode .data .is_agent } 
96+     < button class = " btn btn-primary"   on: click= {handleTestInChat}>< i class = " bx bx-chat" >< / i>  Test in  chat< / button>  
97+     {/ if } 
98+     < button class = " btn btn-primary"   on: click= {handleRunTask}>< i class = " bx bx-run" >< / i>  Run:   
99+         {#if  selectedNode &&  selectedNode .data .task } 
100+             {selectedNode .data .task } 
101+         {: else } 
102+             select a task to run 
103+         {/ if } 
104+     < / button>  
105+ < / div> 
72106< div id= " drawflow"   style= " height: 75vh; width: 100%" > 
73107< / div> 
0 commit comments