0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/plugins/discourse-ai/lib/agents
RnL Dev 3f4a87b135
FEATURE: Add {username} template param to BotContext (#41446)
`BotContext` already tracks the authenticated user who triggered the bot
via `@user` and exposes `username` in `to_json`. This PR makes it
available as a system prompt template variable, consistent with the
existing params like `{participants}` and `{site_url}`.

**What this enables**

Persona authors can now use `{username}` in a system prompt to get the
current user's Discourse username substituted before the prompt reaches
the LLM. The substitution happens on the server, from the authenticated
Discourse session, not from text in the conversation.

One concrete case: a persona that proxies requests to an external
service needs to identify the caller reliably. Without `{username}`, the
LLM has to infer the username from post metadata, which is fragile and
introduces an injection surface. With `{username}`, the persona author
gets the actual authenticated username directly.

**Behavior when no user is present**

When `@user` is nil (report runners, automated pipelines, etc.),
`{username}` is left as a literal token in the prompt. This matches how
`{participants}` and `{resource_url}` behave in contexts where they have
no value. An explicit test covers this.

**Backwards compatibility**

Any persona prompt containing the literal string `{username}` would have
passed through unchanged before this change and will now be substituted.
In practice, no existing persona would have relied on that token staying
literal.

**Changes**

- `lib/agents/bot_context.rb`: add `username` to `TEMPLATE_PARAMS` and
add a `username` method
- `spec/lib/agents/agent_spec.rb`: assert substitution in the existing
render test; add a test covering the no-user case

---

I'm a paying Discourse customer (Pro, Andrea Ross / RnL Solar Inc.,
`community.ripplesandleaves.ca`) and hit this gap building an
integration that needs to pass a verified user identity from Discourse
to an external service. Happy to answer questions.
2026-08-03 12:38:39 -03:00
..
artifact_update_strategies DEV: Enable Style/RedundantBegin rubocop rule (#40096) 2026-05-19 18:44:54 +02:00
tool_runner DEV: Make AI context compaction global (#41317) 2026-07-14 18:41:37 -03:00
tools FEATURE: Add locale-aware AI summaries (#41893) 2026-07-22 17:31:38 +10:00
admin_dashboard_highlights.rb DEV: Default disable some agents (#41076) 2026-06-22 16:38:15 +08:00
agent.rb FEATURE: Add Admin Assistant agent (#41782) 2026-07-21 14:15:36 -03:00
artist.rb
bot.rb FIX: Finalize AI replies at token budget (#41833) 2026-07-20 19:31:58 +10:00
bot_context.rb FEATURE: Add {username} template param to BotContext (#41446) 2026-08-03 12:38:39 -03:00
chat_thread_titler.rb
concept_deduplicator.rb
concept_finder.rb
concept_matcher.rb
content_creator.rb
creative.rb FEATURE: add AI agent thinking effort (#41280) 2026-07-01 17:03:19 -03:00
custom_prompt.rb
designer.rb
discourse_admin_assistant.rb FEATURE: Add Admin Assistant agent (#41782) 2026-07-21 14:15:36 -03:00
discourse_helper.rb FEATURE: add AI agent thinking effort (#41280) 2026-07-01 17:03:19 -03:00
discover.rb
emotion_classifier.rb FEATURE: Add agent-backed sentiment classification (#40136) 2026-05-26 10:32:54 -03:00
forum_researcher.rb FEATURE: add AI agent thinking effort (#41280) 2026-07-01 17:03:19 -03:00
general.rb FEATURE: add AI agent thinking effort (#41280) 2026-07-01 17:03:19 -03:00
github_helper.rb
image_captioner.rb FEATURE: Dedicated image caption AI feature and agent validations (#41729) 2026-07-16 11:29:39 +08:00
locale_detector.rb DEV: Also pass configured supported locales into the prompt for language detection (#40029) 2026-05-14 21:15:26 +08:00
markdown_table_generator.rb
post_illustrator.rb
post_raw_translator.rb FIX: Steer translation agents away from ASCII quotes in structured output (#42131) 2026-07-29 18:36:34 -03:00
proofreader.rb FIX: Don't change quoted content whe proofreading (#38934) 2026-03-27 15:15:25 -03:00
report_runner.rb
researcher.rb
sentiment_classifier.rb FEATURE: Add agent-backed sentiment classification (#40136) 2026-05-26 10:32:54 -03:00
settings_explorer.rb DEV: Add AI tool to read the value of a site setting (#41774) 2026-07-16 12:35:05 -04:00
short_summarizer.rb FEATURE: Add locale-aware AI summaries (#41893) 2026-07-22 17:31:38 +10:00
short_text_translator.rb FIX: Steer translation agents away from ASCII quotes in structured output (#42131) 2026-07-29 18:36:34 -03:00
smart_dates.rb
spam_detector.rb
sql_helper.rb FEATURE: add AI agent thinking effort (#41280) 2026-07-01 17:03:19 -03:00
summarizer.rb
titles_generator.rb
tool_runner.rb DEV: Enable Style/RedundantSelf rubocop rule (#40098) 2026-05-19 19:27:45 +02:00
topic_title_translator.rb FIX: Steer translation agents away from ASCII quotes in structured output (#42131) 2026-07-29 18:36:34 -03:00
translator.rb FIX: Make Translator agent produce faithful translations (#42186) 2026-07-30 15:46:06 -03:00
tutor.rb
web_artifact_creator.rb