Skip to content

fix(ai-chat): send the page's own params where the chat reads them - #2309

Open
Fiona2016 wants to merge 2 commits into
mainfrom
fix/ai-chat-page-params
Open

fix(ai-chat): send the page's own params where the chat reads them#2309
Fiona2016 wants to merge 2 commits into
mainfrom
fix/ai-chat-page-params

Conversation

@Fiona2016

@Fiona2016 Fiona2016 commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Why

In the commercial build the chat is srm-fe's, and a page hands it context through setParamsAiAction. That side (srm-fe ChatContent/index.tsx, formatPageInfo) strips the fields it uses itself and sends everything left in custom as page_from.param — a flat bag, beside workspace_id, business_id and the firemap keys.

We were spreading the wrappers instead of their contents:

  • ...queryPageFrom put {url, param} in whole, so the page's own params landed a level deeper at page_from.param.param, where nothing looks.
  • ...queryAction did the same, so its param overwrote the page's, and its key arrived as a stray field rather than under action — which is the field srm-fe actually destructures and re-adds.

The visible consequence: the metric explorer told the model nothing about which data source the user had selected, so it answered from what metric names usually look like. Measured on 106, asking for a host CPU query returned four candidate expressions and this handed back to the user:

常见的 label 分组字段可能是 instancehostname,取决于采集器配置,建议先用下方语句确认。

That data source has no hostname label — its host identity label is ident.

What changes

Spread queryPageFrom.param, and nest the action under action.

url is deliberately not carried: the chat sets it from the address bar, which names the same page and stays right across a navigation. Checked every AiButton / CustomAiButtonWrap call site — all build queryPageFrom through buildPageFrom({param}), so no caller relied on any top-level field surviving the spread.

Also sends panelKey. Metric.tsx has always compared it to decide whether removing a panel should close the chat, but nothing ever wrote it, so that comparison could never hold. It reaches that comparison in the open-source build only — the commercial one replaces App.tsx and never mounts the provider Metric.tsx reads from; the comment says so.

Testing

FlashAiButton.test.ts now pins both halves of the shape. It asserts against source text rather than rendering, because IS_ENT reads import.meta.env and cannot be evaluated under this project's jest transform — so a rendered test would only ever exercise the CE branch. The file's header says this.

jest src/components/AiChatNG src/pages/explorer — 32 passing. tsc --noEmit matches origin/main exactly (52 pre-existing errors, none in these files).

Related

The backend half is flashcatcloud/fc-model-server#210, which reads these params and delivers the verified query as a card. Neither is useful without the other, but they land independently.

Supersedes #2305, which took a different approach (registering an ai-kit page action) that cannot work in the commercial build — the action would register in n9e/fe's runtime while srm-fe's chat reads its own.

Summary by CodeRabbit

  • Bug Fixes

    • Improved AI chat context handling so page parameters and actions are passed correctly.
    • Preserved drawer behavior when opening AI chat interactions.
    • Prometheus conversations now retain the associated panel context, helping chats identify the panel they belong to.
  • Tests

    • Updated coverage to verify corrected parameter and action handling.

In the commercial build the chat is srm-fe's, and a page hands it context
through setParamsAiAction. That side strips the fields it uses itself and
sends everything left in `custom` as page_from.param — a flat bag, beside
workspace_id and the firemap keys.

We were spreading the wrappers instead of their contents. `queryPageFrom`
went in whole, so the page's params landed a level deeper at param.param
where nothing looks; `queryAction` went in whole too, so its `param`
overwrote the page's and its `key` arrived as a stray field rather than
under `action`. The metric explorer therefore told the model nothing about
which data source the user had selected, and the model answered from what
metric names usually look like.

Spread `queryPageFrom.param` and nest the action under `action`. `url` is
deliberately not carried: the chat reads the address bar, which names the
same page and stays right across a navigation.

Also send `panelKey`. Metric.tsx has always compared it to decide whether
removing a panel should close the chat, but nothing ever wrote it, so that
comparison could never hold.
The commercial build replaces App.tsx and never mounts the provider
Metric.tsx reads, so the close-on-remove it enables is open-source only.
@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Team

Run ID: bceb3fd3-a58d-4357-8552-35bc6d574308

📥 Commits

Reviewing files that changed from the base of the PR and between b69c99a and 6b82b5f.

📒 Files selected for processing (3)
  • src/components/AiChatNG/FlashAiButton.test.ts
  • src/components/AiChatNG/FlashAiButton.tsx
  • src/pages/explorer/Prometheus/index.tsx

Included review availability: Your plan provides up to 2 included reviews per hour; 1 remains after this review.


📝 Walkthrough

Walkthrough

The AI chat handler now builds the expected custom payload from page parameters and action data. Prometheus passes panelKey with page parameters. Tests validate the updated source structure.

Changes

AI chat context

Layer / File(s) Summary
Prometheus panel context
src/pages/explorer/Prometheus/index.tsx
buildPageFrom now includes panelKey in the page parameters passed to AiButton.
AI chat payload mapping and validation
src/components/AiChatNG/FlashAiButton.tsx, src/components/AiChatNG/FlashAiButton.test.ts
useAiEntClickHandler flattens queryPageFrom.param and conditionally assigns queryAction to action. Tests validate the payload shape and forceDrawer ordering.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 6b82b

This change corrects AI chat context payloads so metric explorer page parameters and panel identity are available to chat. No current merge-blocking risk remains.

Suggested reviewers: jsers, 710leo

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: sending the page's own parameters to the AI chat.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/ai-chat-page-params

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant