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/spec/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 FEATURE: Scoped AI highlights to user-selected categories (#41506) 2026-07-09 11:38:30 +08:00
agents FEATURE: Add {username} template param to BotContext (#41446) 2026-08-03 12:38:39 -03:00
ai_bot FEATURE: Approve AI moderation actions inline in bot chat DMs (#41565) 2026-07-13 17:08:35 -03:00
automation FEATURE: extract text from document uploads for LLM prompts (#39634) 2026-05-05 08:16:23 +10:00
completions FIX: Improve vLLM completion handling (#41862) 2026-07-21 16:35:05 +10:00
discord/bot FIX: Run Discord agents as their associated user (#40914) 2026-06-15 18:00:30 -03:00
discourse_automation FIX: Preserve safe formatting in reviewable reasons (#42164) 2026-07-30 17:07:08 +02:00
discourse_workflows/nodes FEATURE: Support AI agent workflow uploads and runner (#41042) 2026-06-19 18:06:17 +02:00
inference
inferred_concepts DEV: AI persona to agent migration (#38319) 2026-03-10 15:59:45 +11:00
mcp DEV: Support other OAuth token auth methods (#41219) 2026-06-26 20:30:53 +08:00
modules FIX: Preserve safe formatting in reviewable reasons (#42164) 2026-07-30 17:07:08 +02: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_spec.rb FEATURE: AI highlights for new dashboard (#40740) 2026-06-10 23:00:35 +08:00
ai_api_audit_log_cleanup_spec.rb FEATURE: Also delete api logs when user / user content is deleted (#41122) 2026-06-24 15:21:00 +08:00
guardian_extensions_spec.rb SECURITY: Force regeneration for edit-outdated summaries and block stale fallback 2026-03-31 15:12:45 +01:00
post_image_captions_spec.rb FEATURE: Dedicated image caption AI feature and agent validations (#41729) 2026-07-16 11:29:39 +08:00
workflow_schema_fields_spec.rb FEATURE: schema based workflow (#41684) 2026-07-15 13:22:29 +02:00