@@ -369,41 +369,31 @@ function resetDetailPanel(): void {
369369// Create app instance
370370const app = new App ( { name : "Customer Segmentation" , version : "1.0.0" } ) ;
371371
372- // Fetch data from server
373- async function fetchData ( ) : Promise < void > {
374- try {
375- const result = await app . callServerTool ( {
376- name : "get-customer-data" ,
377- arguments : { } ,
378- } ) ;
379-
380- const text = result
381- . content ! . filter (
382- ( c ) : c is { type : "text" ; text : string } => c . type === "text" ,
383- )
384- . map ( ( c ) => c . text )
385- . join ( "" ) ;
386- const data = JSON . parse ( text ) as {
387- customers : Customer [ ] ;
388- segments : SegmentSummary [ ] ;
389- } ;
372+ // Handle tool results via structuredContent
373+ app . ontoolresult = ( result ) => {
374+ const data = result . structuredContent as {
375+ customers : Customer [ ] ;
376+ segments : SegmentSummary [ ] ;
377+ } ;
390378
391- state . customers = data . customers ;
392- state . segments = data . segments ;
379+ if ( ! data ?. customers || ! data ?. segments ) {
380+ log . error ( "Invalid data received:" , result ) ;
381+ return ;
382+ }
393383
394- // Initialize or update chart
395- if ( ! state . chart ) {
396- state . chart = initChart ( ) ;
397- } else {
398- updateChart ( ) ;
399- }
384+ state . customers = data . customers ;
385+ state . segments = data . segments ;
400386
401- renderLegend ( ) ;
402- log . info ( `Loaded ${ data . customers . length } customers` ) ;
403- } catch ( error ) {
404- log . error ( "Failed to fetch data:" , error ) ;
387+ // Initialize or update chart
388+ if ( ! state . chart ) {
389+ state . chart = initChart ( ) ;
390+ } else {
391+ updateChart ( ) ;
405392 }
406- }
393+
394+ renderLegend ( ) ;
395+ log . info ( `Loaded ${ data . customers . length } customers` ) ;
396+ } ;
407397
408398// Event handlers
409399xAxisSelect . addEventListener ( "change" , ( ) => {
@@ -482,6 +472,3 @@ app.connect().then(() => {
482472 handleHostContextChanged ( ctx ) ;
483473 }
484474} ) ;
485-
486- // Fetch data after connection
487- setTimeout ( fetchData , 100 ) ;
0 commit comments