Skip to content

Function: useChartFetch()

useChartFetch(chartId): ByIdFetchState<{ chartId: string; error?: string; result?: { chartType: "custom" | "bar" | "horizontalBar" | "line" | "area" | "combo" | "waterfall" | "scatter" | "heatmap" | "radar" | "pie" | "funnel" | "treemap"; option: Record<string, unknown>; }; }>

Fetch a chart by id from the Mastra plugin’s generic /embed/chart/:id endpoint via MastraPluginClient.chart. Used by the chat UI to resolve [chart:<chartId>] markers the agent embeds in prose.

The chart planner runs in the background after prepare_chart mints the id, so the server long-polls the cache until the entry settles (result or error set) and returns the final payload in a single response. The hook performs ONE cached fetch and surfaces:

  • data.result set → render the Echarts spec.
  • data.error set → surface via data.error.
  • data with neither field (planner exceeded the server’s long-poll budget) → render nothing; this answer is NOT cached, so a re-mount will retry.
  • data === undefined (404) → unknown / expired id; render nothing.

string | undefined

ByIdFetchState<{ chartId: string; error?: string; result?: { chartType: "custom" | "bar" | "horizontalBar" | "line" | "area" | "combo" | "waterfall" | "scatter" | "heatmap" | "radar" | "pie" | "funnel" | "treemap"; option: Record<string, unknown>; }; }>