Skip to content

FEATURE: add interactive 2D maths graphs with function-plot - #191

Merged
merefield merged 8 commits into
mainfrom
feature/function-plot-graphs
Oct 26, 2026
Merged

merefield merged 8 commits into
mainfrom
feature/function-plot-graphs

Conversation

@merefield

@merefield merefield commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

Chatbot can pair mathematical explanations with interactive 2D graphs rendered locally using function-plot 1.26.0. The renderer and one plot_function tool ship together, including posts, PMs, previews and Chat.

Supported plot types are documented here and in the README:

plot_type Tool expressions format, one entry per line Limit
function (default) sin(x) for y=f(x) 4 curves
implicit x^2+y^2-4 for F(x,y)=0 2 curves
parametric cos(t); sin(t) for x(t); y(t) 4 curves
polar 2*cos(3*theta) for r(theta) 4 curves
points 1,2 for x,y scatter points 150 points
polyline 1,2 for connected x,y points in supplied order 150 points
vector 3,2 for dx,dy from the origin; 1,1,3,2 for x0,y0,dx,dy 16 vectors

Each graph uses one family. Keep the tool interface to nine fields: expressions, title, plot type, four axis bounds and two optional parameter bounds. The parameter bounds select t/theta for parametric/polar curves and are ignored for other families, including strict-schema placeholders. Axes default to [-10,10]; t/theta defaults to [0,2*pi]. Existing 2D functions fences retain their format. 3D surfaces are unsupported.

  • Keep coloured moving markers and live coordinates for ordinary functions, with snapping within six screen pixels of axis intercepts and series intersections. Hide undefined/off-screen values and clear tracing on exit. All families support pan and zoom; wheel zoom over a graph leaves page scrolling unchanged.
  • Include plot_function in low, medium and high trust defaults while preserving customised lists. chatbot_function_plot_enabled defaults to true and controls rendering and tool eligibility. Use the existing System One tool-selection pipeline.
  • Keep all model prompts, parameter descriptions, correction guidance and reader strings in locale files. Encourage graph output alongside mathematical explanations and the site's usual MathJax notation. Support real cube roots with cbrt(...).
  • Validate the restricted JSON and mathematical grammar on server and client. Pass trusted callbacks to function-plot without evaluating post text as JavaScript, preserving Discourse's CSP. Preserve undefined domain gaps and bound implicit plots to 40000 interval evaluations per redraw; expensive definitions show a fallback.
  • Lazy-load the locally bundled renderer (228019 bytes; 68442 bytes with gzip), with no external rendering service or notional tool quota charge. Include pinned build inputs, licence notices and checksum. Release cached charts, listeners, observers and pending tracing frames on teardown.
  • Document tool syntax, human-authored fenced JSON examples, configuration, limits and fallback behavior in the README. Retain definitions for email, exports and clients without rendering. Plugin version 3.2.0; no database migration.

Tracing applies to ordinary function curves; the other families support pan/zoom. Graphs and snapping are numerical approximations; tangencies can be missed. Pole, discontinuity and coincident-series checks prevent false snapping targets.

Validation:

  • 315 backend examples passed across library, job and migration specs, including the full model/tool/model cycle, eligibility, every plot family, argument validation and unused parameter placeholders.
  • Seven browser flows passed: graph rendering/reload; invalid-expression fallback; moving markers and axis/series snapping; domain gaps; the six additional plot families; implicit evaluation-budget fallback; pan/zoom with wheel containment and normal scrolling outside the graph.
  • 12 QUnit examples passed in Chrome: expression parsing, all graph definitions, interval arithmetic, domain gaps and tracing/intersection edge cases.
  • pnpm lint passed (JavaScript, SCSS, formatting and types). Ruby formatting/RuboCop, YAML parsing and git diff --check passed. The pinned pnpm vendor build reproduced its checksum.

Backend/browser checks used the local Discourse checkout with automation, chat, discourse-local-dates and discourse-chatbot loaded, plus the local plugin-test bootstrap. QUnit used a single-thread Puma test server within the test database connection limit.

@merefield merefield changed the title FEATURE: bundle function-plot graphs and a maths plotting tool FEATURE: add interactive 2D and 3D maths graphs with JSXGraph Oct 26, 2026
@merefield merefield changed the title FEATURE: add interactive 2D and 3D maths graphs with JSXGraph FEATURE: add interactive 2D maths graphs with function-plot Oct 26, 2026
@merefield
merefield requested a lite review from Copilot October 26, 2026 21:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Two critical lockfile findings block reproducible frozen installs, and two moderate client-side correctness findings remain unresolved.

2 open issues
1 file not reviewed
  • public/function-plot/build/pnpm-lock.yaml: Generated file
What changed in this PR

This pull request adds locally rendered, interactive 2D mathematical graphs to Chatbot across posts, PMs, previews, and Chat. It introduces the plot_function tool, secure expression validation, a bundled function-plot renderer, tracing, pan/zoom, fallbacks, documentation, and test coverage.

Changes:

  • Adds function, implicit, parametric, polar, points, polyline, and vector plot support with localized tool guidance and trust-level configuration.
  • Adds lazy-loaded client rendering with CSP-compatible parsing, tracing, snapping, pan/zoom, interval limits, and lifecycle cleanup.
  • Adds pinned vendor build metadata, licensing, documentation, plugin version 3.1.9, and backend/browser/unit tests.

Open findings:

  • Critical, 1 vote: public/function-plot/build/pnpm-lock.yaml omits the direct interval-arithmetic-eval importer dependency, causing documented frozen-lockfile builds to fail.
  • Critical, 3 votes: public/function-plot/build/package.json declares interval-arithmetic-eval without a matching lockfile importer entry, preventing reproducible vendor rebuilds.
  • Moderate, 1 vote: Client title-length validation counts UTF-16 code units rather than Unicode code points, rejecting some valid non-BMP titles.
  • Moderate, 1 vote: Tracing accepts endpoint zeroes before checking discontinuity, allowing false snapping targets at jumps such as floor(x).
File Description
test/​javascripts/​unit/​function-plot-trace-test.js Tests tracing, snapping, intersections, poles, and domain gaps.
test/​javascripts/​unit/​function-plot-renderer-test.js Tests interval evaluation and undefined-domain segmentation.
test/​javascripts/​unit/​function-plot-expression-test.js Tests expression parsing and graph-definition validation.
spec/​system/​read_function_plots_spec.rb Covers rendering, fallbacks, tracing, plot families, and pan/zoom.
spec/​system/​page_objects/​components/​function_plots.rb Provides graph-specific system-test helpers.
spec/​lib/​tools/​function_plot_spec.rb Tests tool arguments, output, validation, graph families, and escaping.
spec/​lib/​site_setting_dependencies_spec.rb Updates expected default tool lists.
spec/​lib/​bot_function_plot_spec.rb Tests tool selection and model/tool/model integration.
README.md Documents graph syntax, limits, behavior, configuration, and fallbacks.
public/​function-plot/​README.md Documents the pinned vendor bundle, rebuild process, licenses, and checksum.
public/​function-plot/​build/​pnpm-lock.yaml Locks vendor build dependencies.
public/​function-plot/​build/​package.json Defines pinned vendor build dependencies and scripts.
public/​function-plot/​build/​entry.js Exposes the renderer and interval arithmetic to the browser.
public/​function-plot/​build/​build.mjs Builds the vendor bundle and generates license notices.
public/​function-plot/​1.26.0/​THIRD-PARTY-LICENSES.txt Provides third-party dependency license notices.
public/​function-plot/​1.26.0/​LICENSE Provides the renderer license.
plugin.rb Bumps the plugin version to 3.1.9.
lib/​discourse_chatbot/​tools/​function_plot.rb Defines the plot_function tool interface and processing.
lib/​discourse_chatbot/​tool.rb Registers plot_function as a built-in tool.
lib/​discourse_chatbot/​function_plot.rb Validates graph inputs and produces fenced graph definitions.
lib/​discourse_chatbot/​bot.rb Adds graph-tool eligibility and feature-setting checks.
config/​settings.yml Adds the rendering setting and trust-level tool defaults.
config/​locales/​server.en.yml Adds tool descriptions, parameter guidance, and correction messages.
config/​locales/​client.en.yml Adds graph loading, fallback, tracing, and help strings.
assets/​stylesheets/​common/​chatbot_common.scss Styles graph containers, legends, coordinates, and errors.
assets/​javascripts/​discourse/​lib/​function-plot-trace.js Implements axis and series-intersection snapping.
assets/​javascripts/​discourse/​lib/​function-plot-renderer.js Integrates the bundled renderer, interval evaluation, graph families, and tracing.
assets/​javascripts/​discourse/​lib/​function-plot-expression.js Parses and validates graph definitions and mathematical expressions.
assets/​javascripts/​discourse/​initializers/​function-plot.js Decorates cooked content and Chat messages with graph components.
assets/​javascripts/​discourse/​components/​function-plot-block.gjs Manages graph rendering, lazy loading, interaction, cleanup, and fallbacks.

Review effort: Lite

Comment thread public/function-plot/build/package.json Resolved
@merefield
merefield merged commit efc364b into main Oct 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants