Skip to content

Conversation

iceljc
Copy link
Collaborator

@iceljc iceljc commented Sep 11, 2025

PR Type

Enhancement


Description

  • Add chat context support to chart generation

  • Include conversation history in chart plotting requests

  • Add configurable message limit for context window

  • Improve instruction template for better report formatting


Diagram Walkthrough

flowchart LR
  A["Routing Context"] --> B["Chart Plot Function"]
  C["Conversation History"] --> B
  B --> D["LLM with Context"]
  D --> E["Generated Chart"]
  F["Message Limit Setting"] --> B
Loading

File Walkthrough

Relevant files
Enhancement
PlotChartFn.cs
Integrate chat context into chart plotting                             

src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs

  • Add IRoutingContext dependency injection for accessing dialog history
  • Retrieve conversation dialogs from routing context or conversation
    service
  • Apply configurable message limit to context window
  • Pass full dialog history to LLM instead of single instruction message
+16/-8   
Configuration changes
ChartHandlerSettings.cs
Add message limit configuration setting                                   

src/Plugins/BotSharp.Plugin.ChartHandler/Settings/ChartHandlerSettings.cs

  • Add MessageLimit property to ChartPlotSetting class
  • Enable configuration of context window size for chart generation
+1/-0     
Documentation
util-chart-plot_instruction.liquid
Enhance report formatting instructions                                     

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

  • Update report summary instructions for better markdown formatting
  • Emphasize proper heading structure and line breaks
  • Improve clarity on formatting requirements
+1/-1     

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Nullability

dialogs is assumed non-null after retrieval from routing and conversation services. If both return null, subsequent calls like TakeLast or Add will throw. Add null checks or default to an empty list.

var dialogs = routingCtx.GetDialogs();
if (dialogs.IsNullOrEmpty())
{
    dialogs = convService.GetDialogHistory();
}

var messageLimit = _settings.ChartPlot?.MessageLimit > 0 ? _settings.ChartPlot.MessageLimit.Value : 50;
dialogs = dialogs.TakeLast(messageLimit).ToList();
dialogs.Add(new RoleDialogModel(AgentRole.User, "Please follow the instruction and chat context to generate valid javascript code.")
{
    CurrentAgentId = message.CurrentAgentId,
    MessageId = message.MessageId
});

var response = await GetChatCompletion(innerAgent, dialogs);
Context Limit

Applying TakeLast(messageLimit) to entire dialog objects may exceed LLM token limits if messages are large. Consider token-based truncation or summarization when close to limits.

var messageLimit = _settings.ChartPlot?.MessageLimit > 0 ? _settings.ChartPlot.MessageLimit.Value : 50;
dialogs = dialogs.TakeLast(messageLimit).ToList();
dialogs.Add(new RoleDialogModel(AgentRole.User, "Please follow the instruction and chat context to generate valid javascript code.")
Output Validity

The stricter markdown instruction (“DO NOT make everything in one line.”) may conflict with JSON encoding of multi-line markdown. Ensure escaping/newlines are handled so the JSON remains valid.

You must output the response in the following JSON format:
{
    "greeting_message": "A short polite message that informs user that the charts have been generated.",
    "js_code": "The javascript code that can generate the charts as requested.",
    "report_summary": "Generate an insightful summary report in markdown format based on the data. You can summarize using one or multiple titles and list the key findings under each title. Bold all key findings and use level-4 headings or smaller (####, #####, etc.). DO NOT make everything in one line."
}

@iceljc iceljc merged commit 6fab3e0 into SciSharp:master Sep 11, 2025
1 of 4 checks passed
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
High-level
Filter and bound chat context

Instead of sending the last N raw dialog messages to the LLM, the suggestion
recommends first sanitizing the history. This involves filtering for relevant
user/assistant messages, removing sensitive data and tool outputs, and then
truncating the context based on token count rather than message count to avoid
overflow and improve security.

Examples:

src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs [49-56]
        var dialogs = routingCtx.GetDialogs();
        if (dialogs.IsNullOrEmpty())
        {
            dialogs = convService.GetDialogHistory();
        }

        var messageLimit = _settings.ChartPlot?.MessageLimit > 0 ? _settings.ChartPlot.MessageLimit.Value : 50;
        dialogs = dialogs.TakeLast(messageLimit).ToList();

Solution Walkthrough:

Before:

public async Task<bool> Execute(RoleDialogModel message)
{
    // ...
    var dialogs = routingCtx.GetDialogs();
    if (dialogs.IsNullOrEmpty())
    {
        dialogs = convService.GetDialogHistory();
    }

    var messageLimit = _settings.ChartPlot?.MessageLimit > 0 ? _settings.ChartPlot.MessageLimit.Value : 50;
    dialogs = dialogs.TakeLast(messageLimit).ToList();
    
    dialogs.Add(new RoleDialogModel(AgentRole.User, "Please follow the instruction..."));

    var response = await GetChatCompletion(innerAgent, dialogs);
    // ...
}

After:

public async Task<bool> Execute(RoleDialogModel message)
{
    // ...
    var rawDialogs = convService.GetDialogHistory();

    // Filter for relevant user/assistant messages and sanitize content
    var sanitizedDialogs = rawDialogs
        .Where(d => d.Role == AgentRole.User || d.Role == AgentRole.Assistant)
        .Select(d => Sanitize(d)) // Strip payloads, redact PII
        .ToList();

    // Truncate based on token count, not message count
    var contextDialogs = TruncateByTokenLimit(sanitizedDialogs, 3000);

    contextDialogs.Add(new RoleDialogModel(AgentRole.User, "Please follow the instruction..."));

    var response = await GetChatCompletion(innerAgent, contextDialogs);
    // ...
}
Suggestion importance[1-10]: 9

__

Why: The suggestion addresses a critical design flaw regarding the handling of conversation history, which has significant security, reliability, and cost implications not considered in the PR.

High
Possible issue
Null-safe message limit handling

Fix potential null dereferences when reading settings and ensure a safe,
non-negative message limit. Also guard against null dialogs before applying
TakeLast to prevent runtime exceptions.

src/Plugins/BotSharp.Plugin.ChartHandler/Functions/PlotChartFn.cs [55-56]

-var messageLimit = _settings.ChartPlot?.MessageLimit > 0 ? _settings.ChartPlot.MessageLimit.Value : 50;
-dialogs = dialogs.TakeLast(messageLimit).ToList();
+var messageLimit = _settings?.ChartPlot?.MessageLimit ?? 50;
+if (messageLimit <= 0) messageLimit = 50;
+dialogs = (dialogs ?? new List<RoleDialogModel>()).TakeLast(messageLimit).ToList();
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies a potential NullReferenceException if _settings.ChartPlot is null and provides a more robust and readable way to handle the message limit logic using null-coalescing and a separate check for non-positive values.

Medium
  • More

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