Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: AI-generated DE queries on /new before creation (#39412)

Natalie Taybcb0949a2fc5
diff --git a/plugins/discourse-ai/config/eval-llms.yml b/plugins/discourse-ai/config/eval-llms.yml
index 6d417585e92..c8ac592b5e4 100644
--- a/plugins/discourse-ai/config/eval-llms.yml
+++ b/plugins/discourse-ai/config/eval-llms.yml
@@ -128,3 +128,13 @@ llms:
url: https://generativelanguage.googleapis.com/v1beta/models/gemini-3-pro-preview
max_prompt_tokens: 1000000
vision_enabled: true
+
+ gemini-3.1-flash-lite:
+ display_name: Gemini 3.1 Flash Lite
+ name: gemini-3.1-flash-lite
+ tokenizer: DiscourseAi::Tokenizer::GeminiTokenizer
+ api_key_env: GEMINI_API_KEY
+ provider: google
+ url: https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash-preview
+ max_prompt_tokens: 1000000
+ vision_enabled: true
diff --git a/plugins/discourse-ai/evals/lib/runners/base.rb b/plugins/discourse-ai/evals/lib/runners/base.rb
index 111d3a7c33d..e2477a858a0 100644
--- a/plugins/discourse-ai/evals/lib/runners/base.rb
+++ b/plugins/discourse-ai/evals/lib/runners/base.rb
@@ -18,6 +18,7 @@ module DiscourseAi
DiscourseAi::Evals::Runners::Spam,
DiscourseAi::Evals::Runners::Summarization,
DiscourseAi::Evals::Runners::Inference,
+ DiscourseAi::Evals::Runners::DataExplorer,
]
klass = registry.find { |runner| runner.can_handle?(feature) }
klass&.new(feature.split(":").last, agent_prompt) if klass
diff --git a/plugins/discourse-ai/evals/lib/runners/data_explorer.rb b/plugins/discourse-ai/evals/lib/runners/data_explorer.rb
new file mode 100644
index 00000000000..ea978c171c3
--- /dev/null
+++ b/plugins/discourse-ai/evals/lib/runners/data_explorer.rb
@@ -0,0 +1,40 @@
+# frozen_string_literal: true
+
+require_relative "base"
+
+module DiscourseAi
+ module Evals
+ module Runners
+ class DataExplorer < Base
+ def self.can_handle?(full_feature_name)
+ full_feature_name&.start_with?("data_explorer:")
+ end
+
+ def run(eval_case, llm, execution_context:)
+ args = eval_case.args
+ agent = resolve_agent(agent_class: DiscourseDataExplorer::AiQueryGenerator)
+ user = Discourse.system_user
+
+ context =
+ DiscourseAi::Agents::BotContext.new(
+ user: user,
+ skip_show_thinking: true,
+ feature_name: "evals/data_explorer_query_generation",
+ messages: [{ type: :user, content: args[:input] }],
+ )
+
+ bot = DiscourseAi::Agents::Bot.as(user, agent: agent, model: llm)
+ sql =
+ capture_structured_response(
+ bot,
+ context,
+ schema_key: "sql",
+ execution_context: execution_context,
+ )
+
+ wrap_result(sql.strip, { feature: feature_name })
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-ai/evals/lib/workbench.rb b/plugins/discourse-ai/evals/lib/workbench.rb
index 1f4a0e079be..ce6069cb855 100644
--- a/plugins/discourse-ai/evals/lib/workbench.rb
+++ b/plugins/discourse-ai/evals/lib/workbench.rb
@@ -11,6 +11,7 @@ require_relative "runners/discoveries"
require_relative "runners/inference"
require_relative "runners/spam"
require_relative "runners/summarization"
+require_relative "runners/data_explorer"
require_relative "judge"
require_relative "agent_prompt_loader"
require_relative "console_formatter"
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
index cdf03c151a2..ddca31574ec 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_agents_controller_spec.rb
@@ -31,7 +31,6 @@ RSpec.describe DiscourseAi::Admin::AiAgentsController do
get "/admin/plugins/discourse-ai/ai-agents.json"
tools = response.parsed_body["meta"]["tools"]
tool_ids = tools.map { |t| t["id"] }
- expect(tool_ids).to include("ValidateSql")
expect(tool_ids).to include("RunSql")
end
diff --git a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs
index 0e42dab5412..5aa2d53cbe0 100644
--- a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs
+++ b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/edit.gjs
@@ -92,13 +92,6 @@ export default class QueriesEdit extends Component {
<div class="clear"></div>
- {{#if @controller.aiGenerating}}
- <div class="query-ai-generating">
- <ConditionalLoadingSpinner @condition={{true}} @size="small" />
- <span>{{i18n "explorer.ai.generating"}}</span>
- </div>
- {{/if}}
-
{{#if @controller.editingQuery}}
<div class="query-editor {{if @controller.hideSchema 'no-schema'}}">
<div class="panels-flex">
@@ -266,7 +259,6 @@ export default class QueriesEdit extends Component {
@action={{@controller.destroyQuery}}
@icon="trash-can"
@label="explorer.delete"
- @disabled={{@controller.aiGenerating}}
class="btn-danger"
/>
{{/if}}
diff --git a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs
index 53c51e40f65..67007aa87f8 100644
--- a/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs
+++ b/plugins/discourse-data-explorer/admin/assets/javascripts/admin/templates/admin-plugins/show/explorer/new.gjs
@@ -1,5 +1,11 @@
+import { on } from "@ember/modifier";
+import AceEditor from "discourse/components/ace-editor";
import BackButton from "discourse/components/back-button";
+import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
+import DButton from "discourse/components/d-button";
+import DTextarea from "discourse/components/d-textarea";
import Form from "discourse/components/form";
+import TextField from "discourse/components/text-field";
import icon from "discourse/helpers/d-icon";
import { i18n } from "discourse-i18n";
@@ -10,67 +16,177 @@ export default <template>
@label="explorer.queries"
/>
- <Form
- @data={{@controller.model}}
- @onSubmit={{@controller.create}}
- class="query-new"
- as |form|
- >
- <form.Field
- @name="name"
- @title={{i18n "explorer.query_name"}}
- @validation="required"
- @format="large"
- @type="input"
- as |field|
- >
- <field.Control />
- </form.Field>
- <form.Field
- @name="description"
- @title={{i18n "explorer.description_placeholder"}}
- @format="large"
- @type="textarea"
- as |field|
- >
- <field.Control />
- </form.Field>
- <form.Actions>
- <form.Submit @label="explorer.create" @icon="plus" />
- </form.Actions>
- </Form>
-
{{#if @controller.aiQueriesEnabled}}
- <div class="query-new__or-divider">
- <span>{{i18n "explorer.ai.or_divider"}}</span>
- </div>
+ <div class="query-new query-new--ai-first">
+ {{#unless @controller.showManualForm}}
+ <div class="query-new__ai-section">
+ <label class="query-new__ai-label">
+ <span>{{i18n "explorer.ai.description_title"}}</span>
+ {{icon "discourse-sparkles"}}
+ </label>
+
+ <p class="query-new__ai-hint">
+ {{i18n "explorer.ai.description_hint"}}
+ </p>
+
+ <DTextarea
+ @value={{@controller.aiDescription}}
+ {{on "input" @controller.updateAiDescription}}
+ {{on "keydown" @controller.handleKeydown}}
+ placeholder={{i18n "explorer.ai.description_placeholder"}}
+ class="query-new__ai-textarea"
+ disabled={{@controller.aiGenerating}}
+ />
+
+ <div class="query-new__ai-actions">
+ {{#if @controller.hasGenerated}}
+ <DButton
+ @action={{@controller.generate}}
+ @icon="discourse-sparkles"
+ @label="explorer.ai.regenerate"
+ @disabled={{@controller.aiGenerating}}
+ class="btn-default query-new__regenerate-btn"
+ />
+ {{else}}
+ <DButton
+ @action={{@controller.generate}}
+ @label="explorer.ai.generate"
+ @disabled={{@controller.aiGenerating}}
+ class="btn-primary query-new__generate-btn"
+ />
+ <DButton
+ @action={{@controller.toggleManualForm}}
+ @label="explorer.ai.write_manually"
+ class="btn-transparent query-new__toggle-link"
+ />
+ {{/if}}
+
+ {{#if @controller.aiGenerating}}
+ <span class="query-new__generating-indicator">
+ <ConditionalLoadingSpinner
+ @condition={{true}}
+ @size="small"
+ />
+ <span>{{i18n "explorer.ai.generating"}}</span>
+ </span>
+ {{/if}}
+ </div>
+ </div>
+
+ {{#if @controller.hasGenerated}}
+ <hr class="query-new__divider" />
+
+ <label class="query-new__field-label">
+ {{i18n "explorer.ai.sql_label"}}
+ </label>
+ <div class="query-new__sql-editor">
+ <AceEditor
+ @content={{@controller.generatedSql}}
+ @onChange={{@controller.updateSql}}
+ @mode="sql"
+ />
+ </div>
+ <div class="query-new__fields">
+ <label class="query-new__field-label">
+ {{i18n "explorer.query_name"}}
+ </label>
+ <TextField
+ @value={{@controller.generatedName}}
+ @onChange={{@controller.updateName}}
+ class="query-new__name-input"
+ />
+
+ <label class="query-new__field-label">
+ {{i18n "explorer.description_placeholder"}}
+ <span class="query-new__optional">
+ ({{i18n "explorer.ai.optional"}})
+ </span>
+ </label>
+ <DTextarea
+ @value={{@controller.generatedDescription}}
+ {{on "input" @controller.updateDescription}}
+ class="query-new__description-input"
+ />
+ </div>
+
+ <div class="query-new__actions">
+ <DButton
+ @action={{@controller.saveQuery}}
+ @label="explorer.ai.save_query"
+ @disabled={{@controller.aiGenerating}}
+ class="btn-default"
+ />
+ </div>
+ {{/if}}
+ {{/unless}}
+
+ {{#if @controller.showManualForm}}
+ <Form
+ @data={{@controller.model}}
+ @onSubmit={{@controller.create}}
+ class="query-new__manual-form"
+ as |form|
+ >
+ <form.Field
+ @name="name"
+ @title={{i18n "explorer.query_name"}}
+ @validation="required"
+ @format="large"
+ @type="input"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ <form.Field
+ @name="description"
+ @title={{i18n "explorer.description_placeholder"}}
+ @format="large"
+ @type="textarea"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ <form.Actions>
+ <form.Submit @label="explorer.create" @icon="plus" />
+ <DButton
+ @action={{@controller.toggleAiForm}}
+ @icon="discourse-sparkles"
+ @label="explorer.ai.description_title"
+ class="btn-transparent query-new__toggle-link"
+ />
+ </form.Actions>
+ </Form>
+ {{/if}}
+ </div>
+ {{else}}
<Form
- @data={{@controller.aiFormData}}
- @onSubmit={{@controller.createWithAi}}
- class="query-new query-new--ai"
+ @data={{@controller.model}}
+ @onSubmit={{@controller.create}}
+ class="query-new"
as |form|
>
- <label class="form-kit__label query-new__ai-label">
- <span>{{i18n "explorer.ai.description_title"}}</span>
- {{icon "discourse-sparkles"}}
- </label>
<form.Field
- @name="ai_description"
- @title={{i18n "explorer.ai.description_title"}}
- @description={{i18n "explorer.ai.description_hint"}}
- @showTitle={{false}}
+ @name="name"
+ @title={{i18n "explorer.query_name"}}
@validation="required"
@format="large"
+ @type="input"
+ as |field|
+ >
+ <field.Control />
+ </form.Field>
+ <form.Field
+ @name="description"
+ @title={{i18n "explorer.description_placeholder"}}
+ @format="large"
@type="textarea"
as |field|
>
- <field.Control
- placeholder={{i18n "explorer.ai.description_placeholder"}}
- />
+ <field.Control />
</form.Field>
<form.Actions>
- <form.Submit @label="explorer.ai.generate" />
+ <form.Submit @label="explorer.create" @icon="plus" />
</form.Actions>
</Form>
{{/if}}
diff --git a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
index 59915dbd2d1..b09d8a95496 100644
--- a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
+++ b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb
@@ -162,24 +162,43 @@ module DiscourseDataExplorer
end
def create
- permitted = %i[name description sql]
- permitted << :ai_description if AiQueryEnqueuer.enabled?
-
- query_params = params.require(:query).permit(*permitted)
- ai_description = query_params.delete(:ai_description)&.strip
+ query_params = params.require(:query).permit(:name, :description, :sql)
group_ids = params.require(:query)[:group_ids]
query =
- QueryCreator.create(
- query_params: query_params,
- ai_description: ai_description,
- group_ids: group_ids,
- user: current_user,
- )
+ QueryCreator.create(query_params: query_params, group_ids: group_ids, user: current_user)
render_serialized query, QueryDetailsSerializer, root: "query"
end
+ def generate_with_ai
+ raise Discourse::NotFound unless AiQueryEnqueuer.enabled?
+ RateLimiter.new(
+ current_user,
+ "data-explorer-ai-generate",
+ 10,
+ 1.minute,
+ apply_limit_to_staff: true,
+ ).performed!
+
+ ai_description = params.require(:ai_description).strip
+ if ai_description.length > 2000
+ raise Discourse::InvalidParameters.new("ai_description is too long (max 2000 characters)")
+ end
+
+ generation_id = SecureRandom.hex
+ existing_sql = params[:existing_sql]&.strip.presence
+
+ AiQueryEnqueuer.enqueue(
+ generation_id: generation_id,
+ user: current_user,
+ ai_description: ai_description,
+ existing_sql: existing_sql,
+ )
+
+ render json: { generation_id: generation_id, status: "generating" }
+ end
+
def update
sql_changed = @query.sql != params.dig(:query, :sql)
diff --git a/plugins/discourse-data-explorer/app/jobs/regular/generate_de_query_with_ai.rb b/plugins/discourse-data-explorer/app/jobs/regular/generate_de_query_with_ai.rb
index 72d3c316f78..38c866a5fe8 100644
--- a/plugins/discourse-data-explorer/app/jobs/regular/generate_de_query_with_ai.rb
+++ b/plugins/discourse-data-explorer/app/jobs/regular/generate_de_query_with_ai.rb
@@ -7,34 +7,33 @@ module Jobs
CHANNEL_PREFIX = "/discourse-data-explorer/queries/ai-generation"
def execute(args)
- @query_id = args[:query_id]
+ @generation_id = args[:generation_id]
return unless SiteSetting.data_explorer_enabled
return unless SiteSetting.data_explorer_ai_queries_enabled
- query = DiscourseDataExplorer::Query.find_by(id: @query_id)
user = User.find_by(id: args[:user_id])
- return if query.nil? || user.nil?
+ return if user.nil?
agent_id =
DiscourseAi::Agents::Agent.external_agent_id(DiscourseDataExplorer::AiQueryGenerator)
agent_record = AiAgent.find_by(id: agent_id)
if agent_record.nil?
- return(
- publish_error(
- query,
- user,
- I18n.t("discourse_data_explorer.ai.error_agent_not_configured"),
- )
- )
+ return(publish_error(user, I18n.t("discourse_data_explorer.ai.error_agent_not_configured")))
end
agent_klass = agent_record.class_instance
bot = DiscourseAi::Agents::Bot.as(Discourse.system_user, agent: agent_klass.new)
+ user_message = args[:ai_description]
+ if args[:existing_sql].present?
+ user_message =
+ "#{args[:ai_description]}\n\nHere is the current SQL query to refine:\n```sql\n#{args[:existing_sql]}\n```"
+ end
+
context =
DiscourseAi::Agents::BotContext.new(
- messages: [{ type: :user, content: args[:ai_description] }],
+ messages: [{ type: :user, content: user_message }],
user: user,
feature_name: "data_explorer_query_generation",
)
@@ -51,20 +50,16 @@ module Jobs
parsed = parse_structured_response(structured_output, result)
if parsed[:sql].blank?
- return(
- publish_error(query, user, I18n.t("discourse_data_explorer.ai.error_no_sql_returned"))
- )
+ return(publish_error(user, I18n.t("discourse_data_explorer.ai.error_no_sql_returned")))
end
- query.update!(
- sql: parsed[:sql].chomp(";").strip,
- name: parsed[:name].presence || args[:ai_description].to_s.truncate(60, separator: " "),
- description: parsed[:description].presence || args[:ai_description],
- )
+ sql = parsed[:sql].chomp(";").strip
+ name = parsed[:name].presence || args[:ai_description].to_s.truncate(60, separator: " ")
+ description = parsed[:description].presence || args[:ai_description]
- publish_complete(query, user)
+ publish_complete(user, sql: sql, name: name, description: description)
rescue => e
- publish_error(query, user, e.message) if query && user
+ publish_error(user, e.message) if user
ensure
cleanup_redis
end
@@ -72,7 +67,9 @@ module Jobs
private
def cleanup_redis
- Discourse.redis.del(DiscourseDataExplorer::AiQueryEnqueuer.redis_key(@query_id)) if @query_id
+ if @generation_id
+ Discourse.redis.del(DiscourseDataExplorer::AiQueryEnqueuer.redis_key(@generation_id))
+ end
end
def parse_structured_response(structured_output, text)
@@ -90,21 +87,27 @@ module Jobs
{ sql: text.strip, name: nil, description: nil }
end
- def publish_complete(query, user)
+ def publish_complete(user, sql:, name:, description:)
MessageBus.publish(
- "#{CHANNEL_PREFIX}/#{query.id}",
- { status: "complete", sql: query.sql, name: query.name, description: query.description },
+ "#{CHANNEL_PREFIX}/#{@generation_id}",
+ {
+ status: "complete",
+ generation_id: @generation_id,
+ sql: sql,
+ name: name,
+ description: description,
+ },
user_ids: [user.id],
max_backlog_age: DiscourseDataExplorer::AiQueryEnqueuer::REDIS_TTL,
)
end
- def publish_error(query, user, message)
- return if query.nil? || user.nil?
+ def publish_error(user, message)
+ return if user.nil?
MessageBus.publish(
- "#{CHANNEL_PREFIX}/#{query.id}",
- { status: "error", error: message },
+ "#{CHANNEL_PREFIX}/#{@generation_id}",
+ { status: "error", generation_id: @generation_id, error: message },
user_ids: [user.id],
max_backlog_age: DiscourseDataExplorer::AiQueryEnqueuer::REDIS_TTL,
)
diff --git a/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_details_serializer.rb b/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_details_serializer.rb
index 18cc41f8582..8abb2eabd4a 100644
--- a/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_details_serializer.rb
+++ b/plugins/discourse-data-explorer/app/serializers/discourse_data_explorer/query_details_serializer.rb
@@ -7,15 +7,5 @@ module DiscourseDataExplorer
def param_info
object&.params&.uniq { |p| p.identifier }&.map(&:to_hash)
end
-
- attribute :ai_generating
-
- def include_ai_generating?
- SiteSetting.data_explorer_ai_queries_enabled
- end
-
- def ai_generating
- Discourse.redis.exists?(DiscourseDataExplorer::AiQueryEnqueuer.redis_key(object.id))
- end
end
end
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjs b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjs
index d9edf899be0..16e6a378d73 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjs
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjs
@@ -425,6 +425,7 @@ export default class QueryResult extends Component {
</table>
</div>
{{/if}}
+
</section>
</article>
</template>
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.js
index c57ba0edc87..7c97f3109b6 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/edit.js
@@ -7,21 +7,13 @@ import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { AUTO_GROUPS } from "discourse/lib/constants";
import { bind } from "discourse/lib/decorators";
-import { i18n } from "discourse-i18n";
import QueryHelp from "discourse/plugins/discourse-data-explorer/discourse/components/modal/query-help";
import { ParamValidationError } from "discourse/plugins/discourse-data-explorer/discourse/components/param-input-form";
import Query from "discourse/plugins/discourse-data-explorer/discourse/models/query";
-const AI_GENERATION_CHANNEL_PREFIX =
- "/discourse-data-explorer/queries/ai-generation";
-const AI_GENERATION_TIMEOUT_MS = 60000;
-
export default class PluginsExplorerController extends Controller {
@service modal;
@service appEvents;
- @service messageBus;
- @service siteSettings;
- @service toasts;
@tracked params;
@tracked editingName = false;
@@ -31,7 +23,6 @@ export default class PluginsExplorerController extends Controller {
@tracked hideSchema = false;
@tracked results = this.model.results;
@tracked dirty = false;
- @tracked aiGenerating = false;
@tracked isCachedResult = false;
queryParams = ["params"];
@@ -39,14 +30,13 @@ export default class PluginsExplorerController extends Controller {
order = null;
form = null;
shouldAutoRun = false;
- _aiGenerationTimer = null;
get saveDisabled() {
- return this.aiGenerating || !this.dirty;
+ return !this.dirty;
}
get runDisabled() {
- return this.aiGenerating || this.dirty;
+ return this.dirty;
}
get parsedParams() {
@@ -61,65 +51,11 @@ export default class PluginsExplorerController extends Controller {
}
get editDisabled() {
- return this.model.is_default || this.aiGenerating;
+ return this.model.is_default;
}
get editorDisabled() {
- return this.model.destroyed || this.aiGenerating;
- }
-
- setupAiGeneration() {
- if (
- !this.siteSettings.data_explorer_ai_queries_enabled ||
- !this.model.ai_generating
- ) {
- return;
- }
-
- this.aiGenerating = true;
- this.editingQuery = true;
- const channel = `${AI_GENERATION_CHANNEL_PREFIX}/${this.model.id}`;
-
- this.messageBus.subscribe(channel, this._onAiGenerationMessage, -1);
-
- this._aiGenerationTimer = setTimeout(() => {
- this._stopAiGeneration();
- this.toasts.error({
- data: { message: i18n("explorer.ai.generation_timeout") },
- });
- }, AI_GENERATION_TIMEOUT_MS);
- }
-
- teardownAiGeneration() {
- if (this.model?.id) {
- const channel = `${AI_GENERATION_CHANNEL_PREFIX}/${this.model.id}`;
- this.messageBus.unsubscribe(channel, this._onAiGenerationMessage);
- }
- if (this._aiGenerationTimer) {
- clearTimeout(this._aiGenerationTimer);
- this._aiGenerationTimer = null;
- }
- }
-
- @bind
- _onAiGenerationMessage(data) {
- if (data.status === "complete") {
- this.model.set("sql", data.sql);
- this.model.set("name", data.name);
- this.model.set("description", data.description);
- this.dirty = false;
- this._stopAiGeneration();
- } else if (data.status === "error") {
- this._stopAiGeneration();
- this.toasts.error({
- data: { message: data.error || i18n("explorer.ai.generation_error") },
- });
- }
- }
-
- _stopAiGeneration() {
- this.aiGenerating = false;
- this.teardownAiGeneration();
+ return this.model.destroyed;
}
get groupOptions() {
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js
index 834ac080425..61d802cc0b7 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/controllers/admin-plugins/show/explorer/new.js
@@ -2,42 +2,120 @@ import { tracked } from "@glimmer/tracking";
import Controller from "@ember/controller";
import { action } from "@ember/object";
import { service } from "@ember/service";
+import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
+import { bind } from "discourse/lib/decorators";
import { i18n } from "discourse-i18n";
+const AI_GENERATION_CHANNEL_PREFIX =
+ "/discourse-data-explorer/queries/ai-generation";
+const AI_GENERATION_TIMEOUT_MS = 60000;
+
export default class AdminPluginsExplorerNew extends Controller {
@service store;
@service router;
@service toasts;
@service siteSettings;
+ @service messageBus;
@tracked loading = false;
- aiFormData = { ai_description: "" };
+ @tracked aiGenerating = false;
+ @tracked hasGenerated = false;
+ @tracked aiDescription = "";
+ @tracked generatedSql = "";
+ @tracked generatedName = "";
+ @tracked generatedDescription = "";
+ @tracked showManualForm = false;
+
+ currentGenerationId = null;
+ _aiGenerationTimer = null;
get aiQueriesEnabled() {
return this.siteSettings.data_explorer_ai_queries_enabled;
}
+ @action
+ handleKeydown(event) {
+ if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
+ event.preventDefault();
+ this.generate();
+ }
+ }
+
+ @action
+ toggleManualForm() {
+ this.showManualForm = true;
+ }
+
+ @action
+ toggleAiForm() {
+ this.showManualForm = false;
+ }
+
@action
async create({ name, description }) {
- await this._createQuery({
- name: name.trim(),
- description: description?.trim(),
- });
+ try {
+ this.loading = true;
+ const result = await this.store
+ .createRecord("query", {
+ name: name.trim(),
+ description: description?.trim(),
+ })
+ .save();
+ this.toasts.success({
+ data: { message: i18n("explorer.query_created") },
+ });
+ this.router.transitionTo(
+ "adminPlugins.show.explorer.edit",
+ result.target.id
+ );
+ } catch (error) {
+ popupAjaxError(error);
+ } finally {
+ this.loading = false;
+ }
}
@action
- async createWithAi({ ai_description }) {
- await this._createQuery({
- name: i18n("explorer.ai.generating_name"),
- ai_description: ai_description.trim(),
- });
+ async generate() {
+ if (this.aiGenerating || !this.aiDescription.trim()) {
+ return;
+ }
+
+ this._teardownMessageBus();
+ this.aiGenerating = true;
+
+ try {
+ const response = await ajax(
+ "/admin/plugins/discourse-data-explorer/queries/generate.json",
+ {
+ type: "POST",
+ data: {
+ ai_description: this.aiDescription.trim(),
+ existing_sql: this.generatedSql || undefined,
+ },
+ }
+ );
+
+ this.currentGenerationId = response.generation_id;
+ this._subscribeToGeneration(this.currentGenerationId);
+ } catch (error) {
+ this.aiGenerating = false;
+ popupAjaxError(error);
+ }
}
- async _createQuery(data) {
+ @action
+ async saveQuery() {
try {
this.loading = true;
- const result = await this.store.createRecord("query", data).save();
+ const result = await this.store
+ .createRecord("query", {
+ name: this.generatedName,
+ description: this.generatedDescription,
+ sql: this.generatedSql,
+ })
+ .save();
this.toasts.success({
data: { message: i18n("explorer.query_created") },
});
@@ -51,4 +129,85 @@ export default class AdminPluginsExplorerNew extends Controller {
this.loading = false;
}
}
+
+ @action
+ updateAiDescription(event) {
+ this.aiDescription = event.target.value;
+ }
+
+ @action
+ updateSql(newSql) {
+ this.generatedSql = newSql;
+ }
+
+ @action
+ updateName(value) {
+ this.generatedName = value;
+ }
+
+ @action
+ updateDescription(event) {
+ this.generatedDescription = event.target.value;
+ }
+
+ _subscribeToGeneration(generationId) {
+ const channel = `${AI_GENERATION_CHANNEL_PREFIX}/${generationId}`;
+ this.messageBus.subscribe(channel, this._onAiGenerationMessage, -1);
+
+ this._aiGenerationTimer = setTimeout(() => {
+ this._teardownMessageBus();
+ this.aiGenerating = false;
+ this.toasts.error({
+ data: { message: i18n("explorer.ai.generation_timeout") },
+ });
+ }, AI_GENERATION_TIMEOUT_MS);
+ }
+
+ @bind
+ _onAiGenerationMessage(data) {
+ if (data.generation_id !== this.currentGenerationId) {
+ return;
+ }
+
+ if (data.status === "complete") {
+ this.generatedSql = data.sql;
+ this.generatedName = data.name;
+ this.generatedDescription = data.description;
+ this.hasGenerated = true;
+ this.aiGenerating = false;
+ this._teardownMessageBus();
+ } else if (data.status === "error") {
+ this.aiGenerating = false;
+ this._teardownMessageBus();
+ this.toasts.error({
+ data: {
+ message: data.error || i18n("explorer.ai.generation_error"),
+ },
+ });
+ }
+ }
+
+ _teardownMessageBus() {
+ if (this.currentGenerationId) {
+ const channel = `${AI_GENERATION_CHANNEL_PREFIX}/${this.currentGenerationId}`;
+ this.messageBus.unsubscribe(channel, this._onAiGenerationMessage);
+ }
+ if (this._aiGenerationTimer) {
+ clearTimeout(this._aiGenerationTimer);
+ this._aiGenerationTimer = null;
+ }
+ }
+
+ resetState() {
+ this._teardownMessageBus();
+ this.currentGenerationId = null;
+ this.aiGenerating = false;
+ this.hasGenerated = false;
+ this.aiDescription = "";
+ this.generatedSql = "";
+ this.generatedName = "";
+ this.generatedDescription = "";
+ this.showManualForm = false;
+ this.loading = false;
+ }
}
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js
index ed06717e43b..0a6e22cf285 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/models/query.js
@@ -74,13 +74,9 @@ export default class Query extends RestModel {
createProperties() {
if (this.sql) {
- // Importing
+ // Importing or saving with SQL
return this.updateProperties();
}
- const props = this.getProperties("name", "description");
- if (this.ai_description) {
- props.ai_description = this.ai_description;
- }
- return props;
+ return this.getProperties("name", "description");
}
}
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js
index 1bfbe327ea1..3a6dba558a8 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/edit.js
@@ -54,8 +54,6 @@ export default class AdminPluginsExplorerQueriesDetails extends DiscourseRoute {
}
setupController(controller, model, transition) {
- controller.teardownAiGeneration();
-
const cachedResult = model.model.cached_result;
const shouldAutoRun = !!transition.to.queryParams.run;
const showCachedResult = !!cachedResult && !shouldAutoRun;
@@ -66,13 +64,7 @@ export default class AdminPluginsExplorerQueriesDetails extends DiscourseRoute {
showResults: showCachedResult,
isCachedResult: showCachedResult,
dirty: false,
- aiGenerating: false,
shouldAutoRun,
});
- controller.setupAiGeneration();
- }
-
- resetController(controller) {
- controller.teardownAiGeneration();
}
}
diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js
index 39db3983be0..06d56279e2d 100644
--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js
+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/routes/admin-plugins/show/explorer/new.js
@@ -4,4 +4,8 @@ export default class AdminPluginsExplorerNew extends DiscourseRoute {
model() {
return { name: "", description: "" };
}
+
+ resetController(controller) {
+ controller.resetState();
+ }
}
diff --git a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
index edfa17daba2..22e049f9fb4 100644
--- a/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
+++ b/plugins/discourse-data-explorer/assets/stylesheets/explorer.scss
@@ -363,30 +363,110 @@ table.group-reports {
margin: 10px 0;
}
-.query-new__or-divider {
- display: flex;
- align-items: center;
- gap: 1em;
- width: var(--form-kit-large-input);
- margin: 1.5em 0;
- color: var(--primary-medium);
- font-size: var(--font-down-1);
- text-transform: uppercase;
- letter-spacing: 0.1em;
+.query-new--ai-first {
+ max-width: 720px;
+
+ .query-new__ai-section {
+ margin-bottom: 1em;
+ }
- &::before,
- &::after {
- content: "";
- flex: 1;
+ .query-new__ai-label {
+ display: flex;
+ align-items: center;
+ gap: 0.35em;
+ font-weight: 700;
+ margin-bottom: 0.25em;
+ }
+
+ .query-new__ai-hint {
+ color: var(--primary-medium);
+ margin-bottom: 0.75em;
+ }
+
+ .query-new__ai-textarea {
+ width: 100%;
+ min-height: 3em;
+ margin-bottom: 0.75em;
+ }
+
+ .query-new__ai-actions {
+ display: flex;
+ align-items: center;
+ gap: 0.5em;
+ }
+
+ .query-new__generating-indicator {
+ display: inline-flex;
+ align-items: center;
+ gap: 0.35em;
+ color: var(--primary-medium);
+ font-size: var(--font-down-1);
+ }
+
+ .query-new__toggle-link {
+ color: var(--tertiary);
+
+ .d-icon {
+ color: var(--tertiary);
+ }
+ }
+
+ .query-new__divider {
+ margin: 1.5em 0;
border-top: 1px solid var(--primary-low-mid);
}
-}
-.query-new__ai-label {
- display: flex;
- align-items: center;
- gap: 0.35em;
- font-weight: 700;
+ .query-new__sql-editor {
+ height: 200px;
+ border: 1px solid var(--primary-low-mid);
+ border-radius: var(--d-border-radius);
+
+ .ace-wrapper {
+ position: relative;
+ height: 100%;
+ width: 100%;
+ }
+
+ .ace_editor {
+ position: absolute;
+ left: 0;
+ right: 0;
+ top: 0;
+ bottom: 0;
+ }
+ }
+
+ .query-new__field-label {
+ display: block;
+ font-weight: 700;
+ margin-bottom: 0.35em;
+ margin-top: 1em;
+
+ &:first-child {
+ margin-top: 0;
+ }
+
+ .query-new__optional {
+ font-weight: 400;
+ color: var(--primary-medium);
+ }
+ }
+
+ .query-new__name-input {
+ width: 100%;
+ margin-bottom: 0;
+ }
+
+ .query-new__description-input {
+ width: 100%;
+ min-height: 60px;
+ }
+
+ .query-new__actions {
+ display: flex;
+ gap: 0.5em;
+ margin-top: 1em;
+ }
}
.query-ai-generating {
@@ -458,7 +538,7 @@ table.group-reports {
.query-results-table-wrapper {
overflow: auto;
- max-height: 1000px;
+ max-height: 300px;
}
.query-results-modes {
diff --git a/plugins/discourse-data-explorer/config/locales/client.en.yml b/plugins/discourse-data-explorer/config/locales/client.en.yml
index 0885a2636e8..247b4c92f14 100644
--- a/plugins/discourse-data-explorer/config/locales/client.en.yml
+++ b/plugins/discourse-data-explorer/config/locales/client.en.yml
@@ -23,15 +23,18 @@ en:
create_placeholder: "Query name..."
description_placeholder: "Enter a description here"
ai:
- or_divider: "OR"
description_title: "Generate with AI"
description_hint: "Describe what you want in plain language. We'll generate the SQL, name, and description for you."
description_placeholder: "e.g. Show me signups across time"
generate: "Generate"
- generating_name: "Generating..."
+ regenerate: "Re-generate"
generating: "Generating query with AI..."
generation_timeout: "AI query generation timed out. You can write the SQL manually."
generation_error: "Something went wrong generating the query."
+ save_query: "Save query"
+ write_manually: "Write SQL manually"
+ sql_label: "SQL"
+ optional: "optional"
import:
label: "Import"
unparseable_json: "Unparseable JSON file."
diff --git a/plugins/discourse-data-explorer/config/locales/server.en.yml b/plugins/discourse-data-explorer/config/locales/server.en.yml
index cf87e720ccd..dc2d817d872 100644
--- a/plugins/discourse-data-explorer/config/locales/server.en.yml
+++ b/plugins/discourse-data-explorer/config/locales/server.en.yml
@@ -9,17 +9,13 @@ en:
name: "Data Explorer Query Generator"
description: "Generates SQL queries for Data Explorer from natural language"
tool_help:
- validate_sql: "Validates a SQL query for use in Data Explorer"
run_sql: "Runs a SQL query and returns results"
tool_summary:
- validate_sql: "Validating SQL query"
run_sql: "Running SQL query"
tool_description:
- validate_sql: "Validated SQL query"
run_sql: "Ran SQL query"
discourse_data_explorer:
ai:
- generating_name: "Generating..."
error_agent_not_configured: "AI agent is not configured. Please check the AI features settings."
error_no_sql_returned: "AI did not return a valid SQL query. Please try again or write the query manually."
discourse_automation:
diff --git a/plugins/discourse-data-explorer/config/routes.rb b/plugins/discourse-data-explorer/config/routes.rb
index 8629a229986..fed808138c8 100644
--- a/plugins/discourse-data-explorer/config/routes.rb
+++ b/plugins/discourse-data-explorer/config/routes.rb
@@ -9,6 +9,7 @@ DiscourseDataExplorer::Engine.routes.draw do
scope "/", defaults: { format: :json } do
get "schema" => "query#schema"
get "groups" => "query#groups"
+ post "queries/generate" => "query#generate_with_ai"
post "queries" => "query#create"
put "queries/:id" => "query#update"
delete "queries/:id" => "query#destroy"
@@ -39,6 +40,8 @@ Discourse::Application.routes.draw do
scope "/", defaults: { format: :json } do
get "/admin/plugins/explorer/schema" => "discourse_data_explorer/query#schema"
get "/admin/plugins/explorer/groups" => "discourse_data_explorer/query#groups"
+ post "/admin/plugins/explorer/queries/generate" =>
+ "discourse_data_explorer/query#generate_with_ai"
post "/admin/plugins/explorer/queries" => "discourse_data_explorer/query#create"
put "/admin/plugins/explorer/queries/:id" => "discourse_data_explorer/query#update"
delete "/admin/plugins/explorer/queries/:id" => "discourse_data_explorer/query#destroy"
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_enqueuer.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_enqueuer.rb
index 79c7b3c4143..c6a021fb34e 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_enqueuer.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_enqueuer.rb
@@ -8,19 +8,20 @@ module DiscourseDataExplorer
SiteSetting.data_explorer_ai_queries_enabled && defined?(DiscourseAi)
end
- def self.redis_key(query_id)
- "data_explorer_ai_generating:#{query_id}"
+ def self.redis_key(generation_id)
+ "data_explorer_ai_generating:#{generation_id}"
end
- def self.enqueue(query:, user:, ai_description:)
+ def self.enqueue(generation_id:, user:, ai_description:, existing_sql: nil)
return if !enabled? || ai_description.blank?
- Discourse.redis.setex(redis_key(query.id), REDIS_TTL, user.id)
+ Discourse.redis.setex(redis_key(generation_id), REDIS_TTL, user.id)
Jobs.enqueue(
:generate_de_query_with_ai,
- query_id: query.id,
+ generation_id: generation_id,
user_id: user.id,
ai_description: ai_description,
+ existing_sql: existing_sql,
)
end
end
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb
index e81182fcda5..5377dbac65f 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/ai_query_generator.rb
@@ -3,11 +3,15 @@
module DiscourseDataExplorer
class AiQueryGenerator < DiscourseAi::Agents::Agent
def tools
- [
- DiscourseAi::Agents::Tools::DbSchema,
- DiscourseDataExplorer::Tools::ValidateSql,
- DiscourseDataExplorer::Tools::RunSql,
- ]
+ [DiscourseAi::Agents::Tools::DbSchema, DiscourseDataExplorer::Tools::RunSql]
+ end
+
+ def self.execution_mode
+ "agentic"
+ end
+
+ def self.max_turn_tokens
+ 100_000
end
def temperature
@@ -26,59 +30,69 @@ module DiscourseDataExplorer
<<~PROMPT
You are a PostgreSQL expert that generates queries for Discourse Data Explorer.
+ ## Workflow
+ 1. Use the schema tool to look up relevant tables for the user's request
+ 2. Write SQL based on the schema
+ 3. Use RunSql to test the query
+ 4. If RunSql returns an error, fix the SQL and test again
+ 5. Repeat until the query runs successfully
+ 6. Return your final response only after confirming the query works
+
Return a JSON response with three fields:
- "name": a short descriptive name for the query (under 60 characters)
- "description": a one-sentence description of what the query does
- - "sql": the SQL query
+ - "sql": the verified SQL query
- SQL rules:
+ ## Data Explorer SQL rules
- NEVER end SQL with a semicolon (;)
- - Format SQL for maximum readability with line breaks, indentation, and spaces around operators
-
- Data Explorer formatting rules:
- - Columns named (user_id, group_id, topic_id, post_id, badge_id) render as links, prefer them where possible
- - You can define custom params for flexible queries:
+ - Format SQL with line breaks, indentation, and spaces around operators
+ - Params are substituted as TEXT strings. You MUST wrap every param in CAST() when using it in date/time expressions or comparisons:
+ WRONG: WHERE created_at >= :start_date
+ WRONG: WHERE created_at < :end_date + INTERVAL '1 day'
+ RIGHT: WHERE created_at >= CAST(:start_date AS date)
+ RIGHT: WHERE created_at < CAST(:end_date AS date) + INTERVAL '1 day'
+ - NEVER use :: to cast params (:: conflicts with :param syntax)
+
+ ## Column rendering
+ Columns named user_id, group_id, topic_id, post_id, badge_id, category_id render as clickable links.
+ Always alias ID columns to these names (e.g. SELECT t.id AS topic_id).
+
+ ## Parameters
+ Declare at top of query:
-- [params]
- -- int :num = 1
- -- text :name
-
- SELECT :num, :name
- - Supported param types: integer, text, boolean, date
-
- Discourse schema knowledge:
- - user_actions table stores likes (action_type 1)
- - topics table stores private/personal messages using archetype 'private_message'
- - notification_level: {muted: 0, regular: 1, tracking: 2, watching: 3, watching_first_post: 4}
- - bookmarkable_type can be: Post, Topic, ChatMessage and more
-
- Current date is: {date}
-
- #{schema_context}
+ -- int :num = 3
+ -- bigint :big = 12345678912345
+ -- boolean :flag
+ -- null boolean :opt_flag = #null
+ -- string :name = default value
+ -- date :date = 14 jul 2015
+ -- time :time = 5:02 pm
+ -- datetime :datetime = 14 jul 2015 5:02 pm
+ -- double :ratio = 3.1415
+ -- user_id :user = system
+ -- post_id :post
+ -- topic_id :topic
+ -- int_list :ids = 1,2,3
+ -- string_list :names = a,b,c
+ -- category_id :category = meta
+ -- group_id :group = admins
+ -- user_list :users = system,discobot
+ -- current_user_id :me
+ Prefix with "null" for optional params.
+ Today is #{Date.today.strftime("%Y-%m-%d")}. Date param defaults MUST be real dates like #{Date.today.strftime("%Y-%m-%d")}, NEVER natural language like "today" or "3 months ago".
+
+ ## Discourse domain knowledge
+ - topics.archetype: 'regular' = topics, 'private_message' = PMs. Default to 'regular'.
+ - Always filter: deleted_at IS NULL
+ - posts.post_number: 1 = OP, > 1 = replies
+ - posts.post_type: 1 = regular, 4 = whisper (exclude from counts)
+ - users.staged = true are email placeholders, exclude unless asked
+ - user_id > 0 excludes system user
+ - user_actions.action_type: 1 = like, 2 = was_liked, 4 = new_topic, 5 = reply
+ - post_actions.post_action_type_id: 2 = like
+
+ Key tables: users, topics, posts, categories, tags, topic_tags, user_actions, post_actions, groups, group_users, user_stats, notifications, bookmarks, badges, user_badges
PROMPT
end
-
- private
-
- def schema_context
- schema = DiscourseAi::Agents::SqlHelper.schema
-
- <<~CONTEXT
- Here is a partial list of tables in the database (you can retrieve schema from these tables as needed)
-
- ```
- #{schema[:other_tables]}
- ```
-
- You may look up schema for the tables listed above.
-
- Here is full information on priority tables:
-
- ```
- #{schema[:priority_tables]}
- ```
-
- NEVER look up schema for the tables listed above, as their full schema is already provided.
- CONTEXT
- end
end
end
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb
index 5c99d577f32..07d4e94c959 100644
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb
+++ b/plugins/discourse-data-explorer/lib/discourse_data_explorer/query_creator.rb
@@ -2,15 +2,9 @@
module DiscourseDataExplorer
class QueryCreator
- def self.create(query_params:, ai_description:, group_ids:, user:)
+ def self.create(query_params:, group_ids:, user:)
name = query_params[:name].presence
- if name.blank?
- if AiQueryEnqueuer.enabled? && ai_description.present?
- name = I18n.t("discourse_data_explorer.ai.generating_name")
- else
- raise Discourse::InvalidParameters.new(:name)
- end
- end
+ raise Discourse::InvalidParameters.new(:name) if name.blank?
attrs = { name: name, user_id: user.id, last_run_at: Time.now }
attrs[:description] = query_params[:description] if query_params[:description].present?
@@ -20,8 +14,6 @@ module DiscourseDataExplorer
group_ids&.each { |group_id| query.query_groups.find_or_create_by!(group_id: group_id) }
- AiQueryEnqueuer.enqueue(query: query, user: user, ai_description: ai_description)
-
query
end
end
diff --git a/plugins/discourse-data-explorer/lib/discourse_data_explorer/tools/validate_sql.rb b/plugins/discourse-data-explorer/lib/discourse_data_explorer/tools/validate_sql.rb
deleted file mode 100644
index 01e7b842400..00000000000
--- a/plugins/discourse-data-explorer/lib/discourse_data_explorer/tools/validate_sql.rb
+++ /dev/null
@@ -1,50 +0,0 @@
-# frozen_string_literal: true
-
-module DiscourseDataExplorer
- module Tools
- class ValidateSql < DiscourseAi::Agents::Tools::Tool
- def self.signature
- {
- name: name,
- description:
- "Validates a SQL query for use in Data Explorer. Checks for prohibited syntax and verifies the query is valid PostgreSQL.",
- parameters: [
- {
- name: "sql",
- description: "the SQL query to validate (without semicolons)",
- type: "string",
- required: true,
- },
- ],
- }
- end
-
- def self.custom?
- true
- end
-
- def self.name
- "validate_sql"
- end
-
- def invoke
- sql = parameters[:sql].to_s.strip
-
- return error_response("SQL query is empty") if sql.blank?
-
- query = DiscourseDataExplorer::Query.new(name: "AI tool validation", sql: sql)
- result = DiscourseDataExplorer::DataExplorer.run_query(query, {}, limit: 0)
-
- return error_response(result[:error].message) if result[:error]
-
- { status: "valid" }
- end
-
- protected
-
- def description_args
- { sql: parameters[:sql].to_s.truncate(100) }
- end
- end
- end
-end
diff --git a/plugins/discourse-data-explorer/plugin.rb b/plugins/discourse-data-explorer/plugin.rb
index 00a73a2490f..f5b22eda1aa 100644
--- a/plugins/discourse-data-explorer/plugin.rb
+++ b/plugins/discourse-data-explorer/plugin.rb
@@ -199,7 +199,6 @@ after_initialize do
end
if defined?(DiscourseAi)
- require_relative "lib/discourse_data_explorer/tools/validate_sql"
require_relative "lib/discourse_data_explorer/tools/run_sql"
require_relative "lib/discourse_data_explorer/ai_query_generator"
diff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
index e048adeb783..292c3ee0acd 100644
--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb
@@ -968,4 +968,83 @@ describe DiscourseDataExplorer::QueryController do
expect(response.parsed_body["rows"]).to eq([[42]])
end
end
+
+ describe "Admin" do
+ fab!(:admin)
+
+ before do
+ sign_in(admin)
+ SiteSetting.data_explorer_enabled = true
+ end
+
+ describe "#generate_with_ai" do
+ before { SiteSetting.data_explorer_ai_queries_enabled = true }
+
+ it "returns 404 when AI queries are disabled" do
+ SiteSetting.data_explorer_ai_queries_enabled = false
+ post "/admin/plugins/discourse-data-explorer/queries/generate.json",
+ params: {
+ ai_description: "show me users",
+ }
+ expect(response.status).to eq(404)
+ end
+
+ it "requires ai_description parameter" do
+ post "/admin/plugins/discourse-data-explorer/queries/generate.json"
+ expect(response.status).to eq(400)
+ end
+
+ it "rejects ai_description over 2000 characters" do
+ post "/admin/plugins/discourse-data-explorer/queries/generate.json",
+ params: {
+ ai_description: "a" * 2001,
+ }
+ expect(response.status).to eq(400)
+ end
+
+ it "enqueues a generation job and returns generation_id" do
+ post "/admin/plugins/discourse-data-explorer/queries/generate.json",
+ params: {
+ ai_description: "show me users",
+ }
+
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+ generation_id = json["generation_id"]
+ expect(generation_id).to be_present
+ expect(json["status"]).to eq("generating")
+
+ job = Jobs::GenerateDeQueryWithAi.jobs.last
+ expect(job["args"].first["generation_id"]).to eq(generation_id)
+ expect(job["args"].first["user_id"]).to eq(admin.id)
+ expect(job["args"].first["ai_description"]).to eq("show me users")
+ end
+
+ it "passes existing_sql when provided" do
+ post "/admin/plugins/discourse-data-explorer/queries/generate.json",
+ params: {
+ ai_description: "refine this query",
+ existing_sql: "SELECT 1",
+ }
+
+ expect(response.status).to eq(200)
+
+ job = Jobs::GenerateDeQueryWithAi.jobs.last
+ expect(job["args"].first["existing_sql"]).to eq("SELECT 1")
+ end
+
+ it "rate limits requests" do
+ RateLimiter.enable
+
+ 11.times do
+ post "/admin/plugins/discourse-data-explorer/queries/generate.json",
+ params: {
+ ai_description: "show me users",
+ }
+ end
+
+ expect(response.status).to eq(429)
+ end
+ end
+ end
end
diff --git a/plugins/discourse-data-explorer/spec/system/ai_query_generation_spec.rb b/plugins/discourse-data-explorer/spec/system/ai_query_generation_spec.rb
index a5e807d80ea..e47b94f737c 100644
--- a/plugins/discourse-data-explorer/spec/system/ai_query_generation_spec.rb
+++ b/plugins/discourse-data-explorer/spec/system/ai_query_generation_spec.rb
@@ -8,51 +8,40 @@ RSpec.describe "Data Explorer AI query generation" do
sign_in admin
end
- # background job publishes this when generation completes.
- # it's internals, but allows us to test re-enablement of the buttons
- def simulate_finish_generating(query, user)
- MessageBus.publish(
- "/discourse-data-explorer/queries/ai-generation/#{query.id}",
- { status: "complete", sql: "SELECT 1", name: "Test Query", description: "A test" },
- user_ids: [user.id],
- )
- end
-
context "when ai queries setting is disabled" do
before { SiteSetting.data_explorer_ai_queries_enabled = false }
- it "does not show the AI description field in the create form" do
+ it "does not show the AI section" do
visit("/admin/plugins/discourse-data-explorer/queries/new")
expect(page).to have_css(".query-new")
- expect(page).to have_no_field("Generate with AI")
+ expect(page).to have_no_css(".query-new--ai-first")
end
end
context "when ai queries setting is enabled" do
before { SiteSetting.data_explorer_ai_queries_enabled = true }
- it "shows the AI description field and creates a query with generating state" do
+ it "shows the AI-first form with generate button" do
visit("/admin/plugins/discourse-data-explorer/queries/new")
- expect(page).to have_css(".query-new--ai")
-
- find(".query-new--ai textarea[name='ai_description']").fill_in(
- with: "show me users who signed up in the last 7 days",
- )
-
- find(".query-new--ai .form-kit__actions .btn-primary").click
+ expect(page).to have_css(".query-new--ai-first")
+ expect(page).to have_css(".query-new__ai-label", text: "Generate with AI")
+ expect(page).to have_css(".query-new__ai-textarea")
+ expect(page).to have_button("Generate")
+ expect(page).to have_button("Write SQL manually")
+ end
- expect(page).to have_current_path(%r{/admin/plugins/discourse-data-explorer/queries/\d+})
- expect(page).to have_css(".query-ai-generating")
- expect(page).to have_button("Run", disabled: true)
- expect(page).to have_button("Delete", disabled: true)
+ it "toggles between AI and manual forms" do
+ visit("/admin/plugins/discourse-data-explorer/queries/new")
- simulate_finish_generating(DiscourseDataExplorer::Query.last, admin)
+ find(".query-new__toggle-link", text: "Write SQL manually").click
+ expect(page).to have_css(".query-new__manual-form")
+ expect(page).to have_no_css(".query-new__ai-section")
- expect(page).to have_no_css(".query-ai-generating")
- expect(page).to have_button("Run", disabled: false)
- expect(page).to have_button("Delete", disabled: false)
+ find(".query-new__toggle-link", text: "Generate with AI").click
+ expect(page).to have_css(".query-new__ai-section")
+ expect(page).to have_no_css(".query-new__manual-form")
end
end
end