mirror of
https://github.com/discourse/discourse.git
synced 2026-08-06 13:08:40 +08:00
Data Explorer's AI query generator produces valid SQL while still
missing the intent behind community/reporting prompts. Existing schema
lookup tells the agent what columns exist, but not how Discourse queries
are usually written or which defaults matter for public activity,
regular members, PMs, staged users, and restricted categories.
This PR adds a `find_queries` tool that lets the agent look up visible
saved queries and bundled defaults before writing SQL. Those queries are
used as examples for patterns, joins, params, and filters, while the
agent still validates the generated SQL with `run_sql`.
The prompt and tool flow are also tightened so the agent submits the
exact SQL that passed validation and handles Data Explorer params more
reliably, especially plural optional filters.
```text
User prompt in Data Explorer
"active users from jan to mar?"
|
v
AiQueryGenerator starts with system prompt
|
v
LLM call #1
decides to call find_queries
|
v
find_queries(search: "active users")
searches visible saved queries + bundled default queries
returns matching examples with:
- name / description
- params
- truncated SQL
|
v
LLM call #2
uses examples to choose relevant tables
|
v
schema(tables: "users,posts,user_visits")
returns real DB columns for requested tables
|
v
LLM call #3
writes SQL using schema + examples
|
v
run_sql(sql: "...")
runs the generated query
returns rows/columns or errors
|
v
LLM call #4
sees run_sql success
|
v
submit_query(name:, description:, sql:)
submits the exact SQL from the successful run_sql call
|
v
Data Explorer receives final generated query
```
|
||
|---|---|---|
| .. | ||
| find_queries_spec.rb | ||
| run_sql_spec.rb | ||
| submit_query_spec.rb | ||