feat(chat): add per-user BYOK integration#808
Open
Delemangi wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends the Discord chat integration to support per-user authenticated chat identities and BYOK (Bring Your Own Key) provider credentials, along with user-scoped model catalog discovery/autocomplete and updated chat payload constraints aligned to the backend contract.
Changes:
- Adds chat-user identity resolution and authenticated, user-scoped requests (including credential CRUD and model catalog caching).
- Introduces
/credentialssubcommands plus a modal submit flow for securely upserting provider API keys. - Updates chat schema/history limits (2,000-char turns, 9 history turns) and improves model handling via catalog-based validation + autocomplete.
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/translations/commands.ts | Adds translated /credentials command descriptions and credential-required error text. |
| src/modules/chat/utils/utils.ts | Simplifies model choice generation to use string model IDs. |
| src/modules/chat/utils/streaming.ts | Truncates saved assistant history turns to 2,000 chars to match schema/Discord limits. |
| src/modules/chat/utils/requests.ts | Adds authenticated credential endpoints, user-scoped model catalog fetching/caching, and inference-model validation. |
| src/modules/chat/utils/reply.ts | Resolves chat user identity, validates configured inference model, and includes userId in chat payload. |
| src/modules/chat/utils/identity.ts | New: resolves Discord users to backend chat identities with caching. |
| src/modules/chat/utils/conversation.ts | Reduces tracked history turns to 9 to match backend/schema constraints. |
| src/modules/chat/utils/constants.ts | Adds SSE error-code mapping for credential_required. |
| src/modules/chat/utils/chatCommand.ts | Ensures slash chat requests resolve identity and include userId; removes system prompt wiring. |
| src/modules/chat/schemas/Model.ts | Replaces hardcoded enum with backend-driven model catalog descriptor schema; updates default model lists. |
| src/modules/chat/schemas/Credentials.ts | New: credential/provider/user schemas and ChatApiError wrapper. |
| src/modules/chat/schemas/Chat.ts | Enforces message/history limits and requires userId (UUID) in outgoing payload. |
| src/modules/chat/commands/modal/credentialSet.ts | New: modal submit handler to upsert provider credentials and invalidate model catalog cache. |
| src/modules/chat/commands/context/prompt.ts | Resolves identity and includes userId; validates configured inference model against catalog. |
| src/modules/chat/commands/chat/credentials.ts | New: /credentials {list,set,delete} command with modal UI for secure key entry. |
| src/modules/chat/commands/chat/chat.ts | Adds inference model autocomplete, uses user-scoped model catalog display, and removes system prompt option. |
| src/modules/chat/commands/autocomplete/chat.ts | New: autocomplete handler sourcing suggestions from the authenticated user model catalog. |
| src/modules/admin/schemas/BotConfig.ts | Loosens inference model config to string; constrains embeddings model to known embedding list. |
| src/core/lib/Command.ts | Adds a first-class ModalCommand type to the command framework. |
| src/core/commands/modules.ts | Adds discovery/registration for modal commands and exposes getModalCommand. |
| src/core/commands/handlers.ts | Implements modal submit routing, permission checks, and analytics tracking for modal commands. |
| src/configuration/bot/defaults.ts | Updates default embedding/inference model IDs to new string-based defaults. |
| README.md | Updates env var descriptions and documents per-user credential management. |
| docs/ARCHITECTURE.md | Documents the new ModalCommand type and discovery directory. |
| config/bot.json.example | Updates example inference model default. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1
to
+10
| import { | ||
| type ChatInputCommandInteraction, | ||
| inlineCode, | ||
| LabelBuilder, | ||
| MessageFlags, | ||
| ModalBuilder, | ||
| SlashCommandBuilder, | ||
| TextInputBuilder, | ||
| TextInputStyle, | ||
| } from 'discord.js'; |
Comment on lines
+99
to
+123
| const modal = new ModalBuilder() | ||
| .setCustomId(`credentialSet:${parsedProvider}`) | ||
| .setTitle(`Постави API клуч за ${parsedProvider}`) | ||
| .addLabelComponents( | ||
| new LabelBuilder() | ||
| .setLabel('API клуч') | ||
| .setTextInputComponent( | ||
| new TextInputBuilder() | ||
| .setCustomId('api-key') | ||
| .setStyle(TextInputStyle.Short) | ||
| .setRequired(true) | ||
| .setMinLength(1) | ||
| .setMaxLength(500), | ||
| ), | ||
| new LabelBuilder() | ||
| .setLabel('Base URL') | ||
| .setDescription('Опционален HTTPS endpoint') | ||
| .setTextInputComponent( | ||
| new TextInputBuilder() | ||
| .setCustomId('base-url') | ||
| .setStyle(TextInputStyle.Short) | ||
| .setRequired(false) | ||
| .setMaxLength(500), | ||
| ), | ||
| ); |
| | `APPLICATION_ID` | Yes | Discord application ID | | ||
| | `CHATBOT_URL` | No | URL of the [`finki-hub/chat-bot`](https://github.com/finki-hub/chat-bot) instance | | ||
| | `API_KEY` | No | API key for authenticated chat bot endpoints, such as embedding endpoints | | ||
| | `API_KEY` | No | API key for authenticated chat bot endpoints, including `/ask` and credential management | |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
Verification
Dependency