import { tool } from "@opencode-ai/plugin";

export default tool({
  description:
    "在前端对话界面中渲染图表。折线图/柱状图必须使用 x_data 作为横轴标签，series[].data 作为同长度的一维数值数组，不要把折线数据写成 ECharts 的 [x, y] 二维点数组。",
  args: {
    reason: tool.schema
      .string()
      .describe("Why this chart should be rendered for the user request."),
    title: tool.schema.string().optional().describe("Chart title."),
    chart_type: tool.schema
      .enum(["line", "bar", "pie"])
      .optional()
      .describe("Chart type."),
    x_data: tool.schema
      .array(tool.schema.string())
      .describe("X-axis labels. For line charts, put time/category labels here."),
    series: tool.schema
      .array(
        tool.schema.object({
          name: tool.schema.string(),
          data: tool.schema
            .array(tool.schema.number())
            .describe("Y values only. Must align by index with x_data."),
          type: tool.schema.enum(["line", "bar"]).optional(),
        }),
      )
      .describe("Series data."),
    x_axis_name: tool.schema
      .string()
      .optional()
      .describe("X-axis display name."),
    y_axis_name: tool.schema
      .string()
      .optional()
      .describe("Y-axis display name."),
  },
  async execute() {
    // 图表数据已经在工具参数里，前端收到 tool_call 后直接渲染，不再二次请求后端。
    return "图表将在对话中显示。";
  },
});
