Skip to content

add chart handler #1085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft

Conversation

iceljc
Copy link
Collaborator

@iceljc iceljc commented Jul 1, 2025

PR Type

Enhancement


Description

  • Add new ChartHandler plugin for AI chart generation

  • Implement JavaScript code template message support

  • Create chart plotting function with LLM integration

  • Add utility hooks and configuration templates


Changes diagram

flowchart LR
  A["User Request"] --> B["ChartHandler Plugin"]
  B --> C["GenerateChartFn"]
  C --> D["LLM Processing"]
  D --> E["JavaScript Code"]
  E --> F["JsCodeTemplateMessage"]
  F --> G["Rich Content Response"]
Loading

Changes walkthrough 📝

Relevant files
Enhancement
8 files
BotSharpMessageParser.cs
Add JsCode message type parsing support                                   
+4/-0     
RichTypeEnum.cs
Add JsCode rich content type constant                                       
+2/-0     
JsCodeTemplateMessage.cs
Create JavaScript code template message class                       
+10/-0   
ChartHandlerPlugin.cs
Implement main chart handler plugin class                               
+18/-0   
UtilityName.cs
Define chart generator utility name constant                         
+6/-0     
GenerateChartFn.cs
Create chart generation function with LLM integration       
+80/-0   
ChartHandlerUtilityHook.cs
Add utility hook for chart generation                                       
+24/-0   
LlmContextIn.cs
Define input context for chart plotting                                   
+10/-0   
Configuration changes
8 files
Using.cs
Add global using statements for plugin                                     
+32/-0   
util-chart-generate_chart.fn.liquid
Add function template for chart generation                             
+1/-0     
util-chart-plot-instruction.liquid
Create chart plotting instruction template                             
+9/-0     
BotSharp.sln
Add ChartHandler project to solution                                         
+11/-0   
BotSharp.Plugin.ChartHandler.csproj
Create project file with dependencies                                       
+29/-0   
util-chart-generate_chart.json
Define chart generation function schema                                   
+14/-0   
WebStarter.csproj
Add ChartHandler plugin reference                                               
+2/-1     
appsettings.json
Enable ChartHandler plugin in configuration                           
+1/-0     
Formatting
2 files
LlmContextIn.cs
Remove commented image URL property                                           
+0/-4     
BotSharp.Plugin.FileHandler.csproj
Clean up project file formatting                                                 
+1/-5     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link

    qodo-merge-pro bot commented Jul 1, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 Security concerns

    Code injection vulnerability:
    The GenerateChartFn generates JavaScript code from LLM output and wraps it in a JsCodeTemplateMessage without any validation or sanitization. This JavaScript code could potentially contain malicious scripts that execute in the user's browser, leading to XSS attacks. The LLM-generated code should be validated, sanitized, or executed in a sandboxed environment before being sent to the client.

    ⚡ Recommended focus areas for review

    Hardcoded Model

    The LLM model is hardcoded to "gpt-4.1" which may not exist or be available in all environments. This should be configurable or use a fallback mechanism.

    var completion = CompletionProvider.GetChatCompletion(_services, provider: "openai", model: "gpt-4.1");
    var response = await completion.GetChatCompletions(agent, dialogs);
    Security Risk

    The function generates JavaScript code from LLM output and returns it directly without validation or sanitization, which could lead to XSS vulnerabilities when executed in the browser.

    Message = new JsCodeTemplateMessage
    {
        Text = response
    }
    Limited Scope

    The instruction template is hardcoded to only generate pie charts, but the function description suggests it can generate charts "in any format that user requested".

    Please take a look at "Requirement" and generate a javascript code that can be used to render a pie chart on a canvas element with id {{ chart_element_id }}.
    

    Copy link

    qodo-merge-pro bot commented Jul 1, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix invalid model name

    The model name "gpt-4.1" appears to be invalid as OpenAI's GPT-4 models
    typically use naming conventions like "gpt-4" or "gpt-4-turbo". This will likely
    cause API failures when attempting to generate chart code.

    src/Plugins/BotSharp.Plugin.ChartHandler/Functions/GenerateChartFn.cs [69]

    -var completion = CompletionProvider.GetChatCompletion(_services, provider: "openai", model: "gpt-4.1");
    +var completion = CompletionProvider.GetChatCompletion(_services, provider: "openai", model: "gpt-4");
    • Apply / Chat
    Suggestion importance[1-10]: 9

    __

    Why: The suggestion correctly identifies that "gpt-4.1" is not a standard OpenAI model name, and using it would likely cause the feature to fail at runtime.

    High
    Add null safety for deserialization

    If message.FunctionArgs is null or invalid JSON, the deserialization will fail
    and throw an exception. Add null checking and exception handling to prevent
    runtime crashes.

    src/Plugins/BotSharp.Plugin.ChartHandler/Functions/GenerateChartFn.cs [28-38]

    -var args = JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs);
    +var args = !string.IsNullOrEmpty(message.FunctionArgs) 
    +    ? JsonSerializer.Deserialize<LlmContextIn>(message.FunctionArgs) 
    +    : null;
     ...
     { "plotting_requirement", args?.PlottingRequirement ?? string.Empty },

    [To ensure code accuracy, apply this suggestion manually]

    Suggestion importance[1-10]: 7

    __

    Why: This is a good defensive coding suggestion that prevents a potential ArgumentNullException if message.FunctionArgs is null, improving the code's robustness.

    Medium
    General
    Remove hardcoded chart type restriction

    The instruction hardcodes "pie chart" but the function accepts any plotting
    requirement. This limits flexibility and may not match user requests for other
    chart types like bar charts or line graphs.

    src/Plugins/BotSharp.Plugin.ChartHandler/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/templates/util-chart-plot-instruction.liquid [1]

    -Please take a look at "Requirement" and generate a javascript code that can be used to render a pie chart on a canvas element with id {{ chart_element_id }}.
    +Please take a look at "Requirement" and generate a javascript code that can be used to render the requested chart on a canvas element with id {{ chart_element_id }}.
    • Apply / Chat
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly points out that hardcoding "pie chart" in the prompt unnecessarily restricts the LLM's output, conflicting with the goal of handling general chart requests.

    Medium
    • More

    @iceljc iceljc marked this pull request as draft July 1, 2025 04:11
    @iceljc iceljc changed the title init add chart handler Jul 1, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant