mirror of
https://github.com/discourse/discourse.git
synced 2026-08-04 10:39:43 +08:00
`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.
|
||
|---|---|---|
| .. | ||
| admin_dashboard | ||
| agents | ||
| ai_bot | ||
| ai_helper | ||
| ai_moderation | ||
| ai_tool_scripts | ||
| automation | ||
| completions | ||
| configuration | ||
| database | ||
| discord/bot | ||
| discover | ||
| embeddings | ||
| inference | ||
| inferred_concepts | ||
| mcp | ||
| sentiment | ||
| summarization | ||
| tasks/modules | ||
| translation | ||
| utils | ||
| admin_dashboard.rb | ||
| ai_api_audit_log_cleaner.rb | ||
| ai_bot.rb | ||
| automation.rb | ||
| embeddings.rb | ||
| engine.rb | ||
| guardian_extensions.rb | ||
| multisite_hash.rb | ||
| post_extensions.rb | ||
| post_image_captions.rb | ||
| summarization.rb | ||
| topic_extensions.rb | ||
| translation.rb | ||
| workflow_schema_fields.rb | ||