Skip to content

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

FEATURE: add thinking animation when thinking blocks are in progress (#36673)

Sam42cc2daf4feb
diff --git a/plugins/discourse-ai/app/models/llm_model.rb b/plugins/discourse-ai/app/models/llm_model.rb
index 2b854b2b905..e1229bb8c6a 100644
--- a/plugins/discourse-ai/app/models/llm_model.rb
+++ b/plugins/discourse-ai/app/models/llm_model.rb
@@ -123,9 +123,11 @@ class LlmModel < ActiveRecord::Base
},
hugging_face: {
disable_system_prompt: :checkbox,
+ disable_native_tools: :checkbox,
},
vllm: {
disable_system_prompt: :checkbox,
+ disable_native_tools: :checkbox,
},
ollama: {
disable_system_prompt: :checkbox,
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js
index 7493a840777..6900b49c484 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/progress-handlers.js
@@ -27,6 +27,26 @@ function lastNonEmptyChild(element) {
return lastChild;
}
+function inProgressThinkingNode(element) {
+ const elem = element.querySelector(".ai-thinking:last-child");
+ if (elem) {
+ // if the next element after this is either empty text or nothing is it in progress
+ let next = elem.nextSibling;
+ while (
+ next &&
+ next.nodeType === Node.TEXT_NODE &&
+ !/\S/.test(next.textContent)
+ ) {
+ next = next.nextSibling;
+ }
+
+ if (!next) {
+ return elem;
+ }
+ }
+ return null;
+}
+
/**
* Adds a progress dot (a span element with a "progress-dot" class) at the end of the
* last non-empty block within a given DOM element. This is used to visually indicate
@@ -34,7 +54,12 @@ function lastNonEmptyChild(element) {
*
* @param {HTMLElement} element - The DOM element to which the progress dot will be added.
*/
-export function addProgressDot(element) {
+export function addProgressDecoration(element) {
+ const thinkingNode = inProgressThinkingNode(element);
+ if (thinkingNode) {
+ thinkingNode.classList.add("in-progress");
+ }
+
let lastBlock = element;
while (true) {
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.js b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.js
index f06ac841933..76283cd792a 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/lib/ai-streamer/updaters/post-updater.js
@@ -1,7 +1,7 @@
import loadMorphlex from "discourse/lib/load-morphlex";
import { withPluginApi } from "discourse/lib/plugin-api";
import { cook } from "discourse/lib/text";
-import { addProgressDot } from "../progress-handlers";
+import { addProgressDecoration } from "../progress-handlers";
import StreamUpdater from "./stream-updater";
export default class PostUpdater extends StreamUpdater {
@@ -53,7 +53,7 @@ export default class PostUpdater extends StreamUpdater {
cookedElement.innerHTML = cooked;
if (!done) {
- addProgressDot(cookedElement);
+ addProgressDecoration(cookedElement);
}
await this.setCooked(cookedElement.innerHTML, done);
diff --git a/plugins/discourse-ai/assets/javascripts/lib/discourse-markdown/ai-tags.js b/plugins/discourse-ai/assets/javascripts/lib/discourse-markdown/ai-tags.js
index 492e5540c46..12d86c296dc 100644
--- a/plugins/discourse-ai/assets/javascripts/lib/discourse-markdown/ai-tags.js
+++ b/plugins/discourse-ai/assets/javascripts/lib/discourse-markdown/ai-tags.js
@@ -1,5 +1,5 @@
export function setup(helper) {
- helper.allowList(["details[class=ai-quote]"]);
+ helper.allowList(["details[class=ai-quote]", "details[class=ai-thinking]"]);
helper.allowList([
"div[class=ai-artifact]",
"div[data-ai-artifact-id]",
diff --git a/plugins/discourse-ai/assets/stylesheets/common/streaming.scss b/plugins/discourse-ai/assets/stylesheets/common/streaming.scss
index bbeb720f722..8fcf3b6ad79 100644
--- a/plugins/discourse-ai/assets/stylesheets/common/streaming.scss
+++ b/plugins/discourse-ai/assets/stylesheets/common/streaming.scss
@@ -34,6 +34,132 @@
@include progress-dot;
}
+details.ai-thinking.in-progress:not([open]) > summary::after {
+ content: "";
+ display: inline-block;
+ inline-size: 1.45em;
+ block-size: 0.35em;
+ margin-inline-start: 0.35em;
+ vertical-align: baseline;
+ position: relative;
+ top: 0.08em;
+ opacity: 0.85;
+ background:
+ radial-gradient(circle, currentcolor 60%, transparent 61%) 0% 100% / 0.25em
+ 0.25em no-repeat,
+ radial-gradient(circle, currentcolor 60%, transparent 61%) 50% 100% / 0.25em
+ 0.25em no-repeat,
+ radial-gradient(circle, currentcolor 60%, transparent 61%) 100% 100% /
+ 0.25em 0.25em no-repeat;
+ animation: ai-thinking-dot-flash 2.8s infinite ease-in-out;
+}
+
+@keyframes ai-thinking-dot-flash {
+ 0%,
+ 12%,
+ 100% {
+ background:
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 0% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 50% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 100% 100% / 0.25em 0.25em no-repeat;
+ }
+
+ 20%,
+ 32% {
+ background:
+ radial-gradient(circle, currentcolor 60%, transparent 61%) 0% 100% /
+ 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 50% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 100% 100% / 0.25em 0.25em no-repeat;
+ }
+
+ 40%,
+ 52% {
+ background:
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 0% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(circle, currentcolor 60%, transparent 61%) 50% 100% /
+ 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 100% 100% / 0.25em 0.25em no-repeat;
+ }
+
+ 60%,
+ 72% {
+ background:
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 0% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 50% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(circle, currentcolor 60%, transparent 61%) 100% 100% /
+ 0.25em 0.25em no-repeat;
+ }
+
+ 80%,
+ 92% {
+ background:
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 0% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 50% 100% / 0.25em 0.25em no-repeat,
+ radial-gradient(
+ circle,
+ color-mix(in srgb, currentcolor 25%, transparent) 60%,
+ transparent 61%
+ )
+ 100% 100% / 0.25em 0.25em no-repeat;
+ }
+}
+
article.streaming .cooked {
.progress-dot::after {
@include progress-dot;
diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml
index 94dffcf9617..f784b0cba60 100644
--- a/plugins/discourse-ai/config/locales/server.en.yml
+++ b/plugins/discourse-ai/config/locales/server.en.yml
@@ -341,7 +341,7 @@ en:
ai_bot:
reply_error: "Sorry, it looks like our system encountered an unexpected issue while trying to reply.\n\n[details='Error details']\n%{details}\n[/details]"
default_pm_prefix: "[Untitled AI bot PM]"
- thinking: "Thinking..."
+ thinking: "Thinking"
personas:
default_llm_required: "Default LLM model is required prior to enabling Chat"
cannot_delete_system_persona: "System personas cannot be deleted, please disable it instead"
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 849816892e3..66b4c81e9e8 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -566,7 +566,7 @@ module DiscourseAi
next if type == :structured_output && !partial.finished?
if should_start_thinking?(partial:, context:, type:, started_thinking:, placeholder:)
- reply << "<details><summary>#{I18n.t("discourse_ai.ai_bot.thinking")}</summary>\n\n"
+ reply << "<details class='ai-thinking'><summary>#{I18n.t("discourse_ai.ai_bot.thinking")}</summary>\n\n"
started_thinking = true
elsif should_stop_thinking?(partial:, context:, type:, started_thinking:, placeholder:)
reply << "</details>\n\n"
diff --git a/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb b/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb
index d8632def6ee..cc50024faaf 100644
--- a/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/chat_gpt.rb
@@ -15,7 +15,7 @@ module DiscourseAi
VALID_ID_REGEX = /\A[a-zA-Z0-9_]+\z/
def native_tool_support?
- llm_model.provider == "open_ai" || llm_model.provider == "azure"
+ !disable_native_tools?
end
def embed_user_ids?
@@ -41,18 +41,18 @@ module DiscourseAi
llm_model.max_prompt_tokens - buffer
end
+ private
+
def disable_native_tools?
return @disable_native_tools if defined?(@disable_native_tools)
!!@disable_native_tools = llm_model.lookup_custom_param("disable_native_tools")
end
- private
-
def tools_dialect
- if disable_native_tools?
- super
- else
+ if native_tool_support?
@tools_dialect ||= DiscourseAi::Completions::Dialects::OpenAiTools.new(prompt.tools)
+ else
+ super
end
end
@@ -72,7 +72,7 @@ module DiscourseAi
def system_msg(msg)
content = msg[:content]
- if disable_native_tools? && tools_dialect.instructions.present?
+ if !native_tool_support? && tools_dialect.instructions.present?
content = content + "\n\n" + tools_dialect.instructions
end
@@ -90,18 +90,18 @@ module DiscourseAi
end
def tool_call_msg(msg)
- if disable_native_tools?
- super
- else
+ if native_tool_support?
tools_dialect.from_raw_tool_call(msg)
+ else
+ super
end
end
def tool_msg(msg)
- if disable_native_tools?
- super
- else
+ if native_tool_support?
tools_dialect.from_raw_tool(msg)
+ else
+ super
end
end
diff --git a/plugins/discourse-ai/lib/completions/dialects/mistral.rb b/plugins/discourse-ai/lib/completions/dialects/mistral.rb
index eaf5eab8229..ba806f162ff 100644
--- a/plugins/discourse-ai/lib/completions/dialects/mistral.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/mistral.rb
@@ -12,6 +12,10 @@ module DiscourseAi
end
end
+ def native_tool_support?
+ !disable_native_tools?
+ end
+
def translate
corrected = super
corrected.each do |msg|
diff --git a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
index 945ace2f65d..ff06d52bbe7 100644
--- a/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
+++ b/plugins/discourse-ai/lib/completions/dialects/open_ai_responses.rb
@@ -14,7 +14,7 @@ module DiscourseAi
VALID_ID_REGEX = /\A[a-zA-Z0-9_]+\z/
def native_tool_support?
- true
+ !disable_native_tools?
end
def max_prompt_tokens
@@ -32,13 +32,13 @@ module DiscourseAi
hoist_reasoning(super)
end
+ private
+
def disable_native_tools?
return @disable_native_tools if defined?(@disable_native_tools)
!!@disable_native_tools = llm_model.lookup_custom_param("disable_native_tools")
end
- private
-
def tools_dialect
if disable_native_tools?
super
diff --git a/plugins/discourse-ai/lib/completions/endpoints/open_ai.rb b/plugins/discourse-ai/lib/completions/endpoints/open_ai.rb
index dcfb73ce29f..db062bb3487 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/open_ai.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/open_ai.rb
@@ -50,7 +50,7 @@ module DiscourseAi
cancel_manager: nil,
&blk
)
- @disable_native_tools = dialect.disable_native_tools?
+ @native_tool_support = dialect.native_tool_support?
super
end
@@ -156,7 +156,7 @@ module DiscourseAi
end
def xml_tools_enabled?
- !!@disable_native_tools
+ !@native_tool_support
end
private
diff --git a/plugins/discourse-ai/lib/completions/endpoints/open_ai_responses.rb b/plugins/discourse-ai/lib/completions/endpoints/open_ai_responses.rb
index 11aed3387ff..cfed7749f4e 100644
--- a/plugins/discourse-ai/lib/completions/endpoints/open_ai_responses.rb
+++ b/plugins/discourse-ai/lib/completions/endpoints/open_ai_responses.rb
@@ -41,7 +41,7 @@ module DiscourseAi
cancel_manager: nil,
&blk
)
- @disable_native_tools = dialect.disable_native_tools?
+ @native_tool_support = dialect.native_tool_support?
super
end
@@ -160,7 +160,7 @@ module DiscourseAi
end
def xml_tools_enabled?
- !!@disable_native_tools
+ !@native_tool_support
end
def processor
diff --git a/plugins/discourse-ai/lib/personas/bot.rb b/plugins/discourse-ai/lib/personas/bot.rb
index 17c4b247c2b..428344ff62e 100644
--- a/plugins/discourse-ai/lib/personas/bot.rb
+++ b/plugins/discourse-ai/lib/personas/bot.rb
@@ -187,7 +187,8 @@ module DiscourseAi
total_completions += 1
# do not allow tools when we are at the end of a chain (total_completions == MAX_COMPLETIONS - 1)
- prompt.tool_choice = :none if total_completions == MAX_COMPLETIONS - 1
+ prompt.tool_choice = :none if total_completions == MAX_COMPLETIONS - 1 ||
+ tools_ran >= MAX_TOOLS
end
embed_thinking(raw_context)
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
index b198741184e..cc5d71fb915 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
@@ -187,7 +187,7 @@ RSpec.describe DiscourseAi::AiBot::Playground do
end
expected = <<~TXT.strip
- <details><summary>#{I18n.t("discourse_ai.ai_bot.thinking")}</summary>
+ <details class='ai-thinking'><summary>#{I18n.t("discourse_ai.ai_bot.thinking")}</summary>
**searching for things**
did stuff
diff --git a/plugins/discourse-ai/spec/requests/admin/ai_personas_controller_spec.rb b/plugins/discourse-ai/spec/requests/admin/ai_personas_controller_spec.rb
index 1b61a515234..9077a28e296 100644
--- a/plugins/discourse-ai/spec/requests/admin/ai_personas_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/admin/ai_personas_controller_spec.rb
@@ -968,7 +968,8 @@ RSpec.describe DiscourseAi::Admin::AiPersonasController do
expect(last_post.raw).to end_with("this is the response after the tool")
# function call is visible in the post
- expect(last_post.raw[0..8]).to eq("<details>")
+ expect(last_post.raw[0..8]).to eq("<details ")
+ expect(last_post.raw).to include("ai-thinking")
user_post = topic.posts.find_by(post_number: 3)
expect(user_post.raw).to eq("how are you now?")
diff --git a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
index 1fcd6b02a4e..35ffdbb3115 100644
--- a/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
+++ b/plugins/discourse-ai/spec/system/llms/ai_llm_spec.rb
@@ -107,12 +107,14 @@ RSpec.describe "Managing LLM configurations", type: :system do
form.field("provider").select("vllm")
expect(form).to have_field_with_name("provider_params.disable_system_prompt")
- expect(form).to have_no_field_with_name("provider_params.disable_native_tools")
+ expect(form).to have_field_with_name("provider_params.disable_native_tools")
- form.field("provider").select("open_router")
+ form.field("provider").select("aws_bedrock")
- expect(form).to have_field_with_name("provider_params.disable_streaming")
+ # missing option from bedrock, maybe we should add
+ expect(form).to have_no_field_with_name("provider_params.disable_streaming")
expect(form).to have_field_with_name("provider_params.disable_native_tools")
+ expect(form).to have_field_with_name("provider_params.effort")
end
it "updates if the url can be edited" do
diff --git a/plugins/discourse-ai/test/javascripts/unit/lib/ai-streamer-test.js b/plugins/discourse-ai/test/javascripts/unit/lib/ai-streamer-test.js
index f3d7cdaaacc..b1ccaa5b8d1 100644
--- a/plugins/discourse-ai/test/javascripts/unit/lib/ai-streamer-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/lib/ai-streamer-test.js
@@ -1,6 +1,6 @@
import { module, test } from "qunit";
import {
- addProgressDot,
+ addProgressDecoration,
applyProgress,
MIN_LETTERS_PER_INTERVAL,
} from "discourse/plugins/discourse-ai/discourse/lib/ai-streamer/progress-handlers";
@@ -54,11 +54,29 @@ module("Discourse AI | Unit | Lib | ai-streamer", function () {
const expectedElement = document.createElement("div");
expectedElement.innerHTML = expected;
- addProgressDot(element);
+ addProgressDecoration(element);
assert.equal(element.innerHTML, expectedElement.innerHTML);
}
+ test("it correctly decorates thinking blocks at end of stream", function (assert) {
+ const html =
+ "<details class='ai-thinking'><summary>Thinking ...</summary><p>test</p></details>";
+ const expected =
+ "<details class='ai-thinking in-progress'><summary>Thinking ...</summary><p>test<span class='progress-dot'></span></p></details>";
+
+ confirmPlaceholder(html, expected, assert);
+ });
+
+ test("it does not decorate thinking blocks at middle of stream", function (assert) {
+ const html =
+ "<details class='ai-thinking'><summary>Thinking ...</summary><p>test</p></details><p>more content</p>";
+ const expected =
+ "<details class='ai-thinking'><summary>Thinking ...</summary><p>test</p></details><p>more content<span class='progress-dot'></span></p>";
+
+ confirmPlaceholder(html, expected, assert);
+ });
+
test("inserts progress span in correct location for simple div", function (assert) {
const html = "<div>hello world<div>hello 2</div></div>";
const expected =