Skip to content

feat(chat): add per-user BYOK integration#808

Open
Delemangi wants to merge 3 commits into
mainfrom
feat/discord-byok-integration
Open

feat(chat): add per-user BYOK integration#808
Delemangi wants to merge 3 commits into
mainfrom
feat/discord-byok-integration

Conversation

@Delemangi

Copy link
Copy Markdown
Member

Summary

  • authenticate chat requests and resolve Discord users to backend chat identities
  • add ephemeral provider credential management with modal input
  • consume the authenticated, user-scoped model catalog with autocomplete and stale-model fallback
  • align request history, message limits, model defaults, and SSE errors with the backend contract
  • add first-class modal command registration and update chat integration documentation

Verification

  • npm run check
  • npm run lint
  • npm run build
  • manual identity, credential lifecycle, model catalog, SSE success, and credential-required QA

Dependency

Copilot AI review requested due to automatic review settings July 12, 2026 23:11
@Delemangi Delemangi self-assigned this Jul 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 /credentials subcommands 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),
),
);
Comment thread README.md
| `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 |
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
6.7% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants