@@ -4,10 +4,10 @@ import {
44 randomUUID ,
55 type RunAgentInput ,
66} from '@ag-ui/client' ;
7+ import { getExtendedLocalAgent } from '@internal/ag-ui-server' ;
78import type { ContextWithMastra } from '@mastra/core/server' ;
89import { streamSSE } from 'hono/streaming' ;
910
10- import { getExtendedLocalAgent } from '../../../../libs/ag-ui-server/index.js' ;
1111import {
1212 computeDashboardRequestHash ,
1313 type DashboardCacheEntry ,
@@ -35,10 +35,10 @@ const DASHBOARD_AGENT_ID = 'dashboardAgent';
3535const CACHED_FRAME_DELAY_MS = resolveCachedFrameDelayMs ( ) ;
3636
3737function resolveCachedFrameDelayMs ( ) : number | null {
38- if ( process . env . NODE_ENV === 'production' ) {
38+ if ( process . env [ ' NODE_ENV' ] === 'production' ) {
3939 return null ;
4040 }
41- const raw = process . env . AG_UI_STREAM_FRAME_DELAY_MS ;
41+ const raw = process . env [ ' AG_UI_STREAM_FRAME_DELAY_MS' ] ;
4242 if ( raw === undefined ) {
4343 return 0 ;
4444 }
@@ -78,62 +78,67 @@ export async function dashboardAgUiRouteHandler(
7878 requestContext,
7979 } ) ;
8080
81- return streamSSE ( c , async ( sse ) => {
82- let renderToolCallId : string | undefined ;
83- let argsBuffer = '' ;
84- let capturedSpec : DashboardSpec | undefined ;
85-
86- await streamAgentEvents ( sse , agent , input , {
87- onEvent : async ( event ) : Promise < readonly BaseEvent [ ] | void > => {
88- const e = event as BaseEvent & {
89- toolCallId ?: string ;
90- toolCallName ?: string ;
91- delta ?: string ;
92- } ;
93-
94- if (
95- e . type === EventType . TOOL_CALL_START &&
96- e . toolCallName === RENDER_DASHBOARD_TOOL_NAME &&
97- typeof e . toolCallId === 'string'
98- ) {
99- renderToolCallId = e . toolCallId ;
100- argsBuffer = '' ;
101- return ;
102- }
81+ // `c` is typed against @mastra/core's bundled hono, which is structurally
82+ // incompatible with the project's hono `Context` that `streamSSE` expects.
83+ return streamSSE (
84+ c as unknown as Parameters < typeof streamSSE > [ 0 ] ,
85+ async ( sse ) => {
86+ let renderToolCallId : string | undefined ;
87+ let argsBuffer = '' ;
88+ let capturedSpec : DashboardSpec | undefined ;
89+
90+ await streamAgentEvents ( sse , agent , input , {
91+ onEvent : async ( event ) : Promise < readonly BaseEvent [ ] | void > => {
92+ const e = event as BaseEvent & {
93+ toolCallId ?: string ;
94+ toolCallName ?: string ;
95+ delta ?: string ;
96+ } ;
97+
98+ if (
99+ e . type === EventType . TOOL_CALL_START &&
100+ e . toolCallName === RENDER_DASHBOARD_TOOL_NAME &&
101+ typeof e . toolCallId === 'string'
102+ ) {
103+ renderToolCallId = e . toolCallId ;
104+ argsBuffer = '' ;
105+ return ;
106+ }
103107
104- if (
105- e . type === EventType . TOOL_CALL_ARGS &&
106- e . toolCallId === renderToolCallId &&
107- typeof e . delta === 'string'
108- ) {
109- argsBuffer += e . delta ;
110- return ;
111- }
108+ if (
109+ e . type === EventType . TOOL_CALL_ARGS &&
110+ e . toolCallId === renderToolCallId &&
111+ typeof e . delta === 'string'
112+ ) {
113+ argsBuffer += e . delta ;
114+ return ;
115+ }
112116
113- if (
114- e . type === EventType . TOOL_CALL_END &&
115- e . toolCallId === renderToolCallId
116- ) {
117- const { events, spec } = await handleRenderToolCallEnd ( argsBuffer ) ;
118- if ( spec ) {
119- capturedSpec = spec ;
117+ if (
118+ e . type === EventType . TOOL_CALL_END &&
119+ e . toolCallId === renderToolCallId
120+ ) {
121+ const { events, spec } = await handleRenderToolCallEnd ( argsBuffer ) ;
122+ if ( spec ) {
123+ capturedSpec = spec ;
124+ }
125+ return events ;
120126 }
121- return events ;
127+ } ,
128+ } ) ;
129+
130+ if ( capturedSpec && ! preventCaching ) {
131+ try {
132+ await writeDashboardCache ( cacheKey , capturedSpec ) ;
133+ } catch ( err ) {
134+ console . error (
135+ `Failed to write dashboard cache (hash=${ cacheKey } ):` ,
136+ err ,
137+ ) ;
122138 }
123- } ,
124- } ) ;
125-
126- if ( capturedSpec && ! preventCaching ) {
127- try {
128- await writeDashboardCache ( cacheKey , capturedSpec ) ;
129- } catch ( err ) {
130- console . error (
131- `Failed to write dashboard cache (hash=${ cacheKey } ):` ,
132- err ,
133- ) ;
134139 }
135- }
136- } ) ;
140+ } ,
141+ ) ;
137142}
138143
139144async function streamCachedDashboard (
@@ -324,9 +329,14 @@ async function tryServeFromCache(
324329 if ( ! entry ) {
325330 return null ;
326331 }
327- return streamSSE ( c , async ( sse ) => {
328- await streamCachedDashboard ( sse , input , entry . spec ) ;
329- } ) ;
332+ // `c` is typed against @mastra/core's bundled hono, which is structurally
333+ // incompatible with the project's hono `Context` that `streamSSE` expects.
334+ return streamSSE (
335+ c as unknown as Parameters < typeof streamSSE > [ 0 ] ,
336+ async ( sse ) => {
337+ await streamCachedDashboard ( sse , input , entry . spec ) ;
338+ } ,
339+ ) ;
330340}
331341
332342interface RenderToolCallEndResult {
0 commit comments