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
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
..
admin_dashboard FIX: Split browser pageview rollup source at the cutover date (#42013) 2026-07-24 14:56:36 +08:00
agents FEATURE: Add {username} template param to BotContext (#41446) 2026-08-03 12:38:39 -03:00
ai_bot SECURITY: Scope AI bot reply stream to the PM's participants 2026-07-28 16:40:22 +02:00
ai_helper PERF: Speed up AI helper diff streaming (#41787) 2026-07-17 10:17:54 -03:00
ai_moderation DEV: Enable Style/RedundantSelf rubocop rule (#40098) 2026-05-19 19:27:45 +02:00
ai_tool_scripts
automation FIX: Avoid duplicate AI triage reviewables when review precedes spam (#41079) 2026-06-22 15:48:56 +08:00
completions FIX: Improve vLLM completion handling (#41862) 2026-07-21 16:35:05 +10:00
configuration FEATURE: Dedicated image caption AI feature and agent validations (#41729) 2026-07-16 11:29:39 +08:00
database
discord/bot FIX: Run Discord agents as their associated user (#40914) 2026-06-15 18:00:30 -03:00
discover
embeddings
inference
inferred_concepts
mcp
sentiment DEV: Let plugins exclude reports from the customisable dashboard (#40463) 2026-06-03 16:36:01 +03:00
summarization FEATURE: Add locale-aware AI summaries (#41893) 2026-07-22 17:31:38 +10:00
tasks/modules DEV: Remove rake task (#42157) 2026-07-30 16:37:37 +08:00
translation PERF: Remove ai_translation_max_post_length setting (#42133) 2026-07-30 19:35:58 +08:00
utils FIX: Recover AI structured output when models emit stray prefixes (#41745) 2026-07-15 13:48:23 -03:00
admin_dashboard.rb FEATURE: AI highlights for new dashboard (#40740) 2026-06-10 23:00:35 +08:00
ai_api_audit_log_cleaner.rb FEATURE: Also delete api logs when user / user content is deleted (#41122) 2026-06-24 15:21:00 +08:00
ai_bot.rb FIX: Allow AI bot conversations when PM creation is restricted (#41232) 2026-06-29 11:06:39 -05:00
automation.rb FIX: Preserve safe formatting in reviewable reasons (#42164) 2026-07-30 17:07:08 +02:00
embeddings.rb
engine.rb
guardian_extensions.rb FIX: Allow AI bot conversations when PM creation is restricted (#41232) 2026-06-29 11:06:39 -05:00
multisite_hash.rb
post_extensions.rb
post_image_captions.rb FEATURE: Dedicated image caption AI feature and agent validations (#41729) 2026-07-16 11:29:39 +08:00
summarization.rb FEATURE: Allow selection of multiple content languages when viewing topics and posts (#42128) 2026-07-30 12:19:44 +08:00
topic_extensions.rb FEATURE: Add locale-aware AI summaries (#41893) 2026-07-22 17:31:38 +10:00
translation.rb UX: Convert AI translation's max days ago setting to a specific date setting instead (#42082) 2026-07-28 18:27:34 +08:00
workflow_schema_fields.rb FEATURE: schema based workflow (#41684) 2026-07-15 13:22:29 +02:00