Skip to content

Commit 010e460

Browse files
📝 Add docstrings to refactor/triage-prompt-rewrite
Docstrings generation was requested by @BillChirico. The following files were modified: * `src/modules/triage-prompt.js` * `src/modules/triage-respond.js` * `web/src/components/dashboard/config-editor.tsx` * `web/src/components/dashboard/config-sections/ChallengesSection.tsx` * `web/src/components/dashboard/config-sections/CommunityFeaturesSection.tsx` * `web/src/components/dashboard/config-sections/GitHubSection.tsx` * `web/src/components/dashboard/config-sections/MemorySection.tsx` * `web/src/components/dashboard/config-sections/PermissionsSection.tsx` * `web/src/components/dashboard/config-sections/StarboardSection.tsx` * `web/src/components/dashboard/config-sections/TicketsSection.tsx` * `web/src/components/dashboard/config-sections/TriageSection.tsx` * `web/src/components/dashboard/config-sections/WelcomeSection.tsx` * `web/src/lib/config-normalization.ts` * `web/src/lib/config-updates.ts` These files were kept as they were: * `src/modules/triage.js` * `web/src/components/dashboard/config-sections/AiAutoModSection.tsx` * `web/src/components/dashboard/config-sections/AiSection.tsx` * `web/src/components/dashboard/config-sections/EngagementSection.tsx` * `web/src/components/dashboard/config-sections/ModerationSection.tsx` * `web/src/components/dashboard/config-sections/ReputationSection.tsx` These files were ignored: * `tests/modules/triage-prompt.test.js` * `tests/modules/triage.test.js` * `web/tests/components/dashboard/config-editor-autosave.test.tsx` * `web/tests/lib/config-normalization.test.ts` * `web/tests/lib/config-updates.test.ts` These file types are not supported: * `.github/workflows/maintain-docs.md` * `src/prompts/community-rules.md` * `src/prompts/search-guardrails.md` * `src/prompts/triage-classify-system.md` * `src/prompts/triage-classify.md` * `src/prompts/triage-respond-system.md` * `src/prompts/triage-respond.md`
1 parent de6eae4 commit 010e460

14 files changed

+161
-83
lines changed

src/modules/triage-prompt.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ export function escapePromptDelimiters(text) {
2727
// ── Conversation text formatting ─────────────────────────────────────────────
2828

2929
/**
30-
* Build conversation text with message IDs for prompts.
31-
* Splits output into <recent-history> (context) and <messages-to-evaluate> (buffer).
32-
* Includes timestamps and reply context when available.
30+
* Build a structured conversation text for prompts including optional channel metadata.
3331
*
34-
* User-supplied content (message body and reply excerpts) is passed through
35-
* {@link escapePromptDelimiters} to neutralise prompt-injection attempts.
32+
* Produces sections: an optional <channel-context> (Channel and optional Topic) taken from the first entry that contains channel metadata, a <recent-history> block for `context` messages (when present), and a <messages-to-evaluate> block for `buffer` messages. Each message line contains an optional timestamp, messageId, author, user mention, optional reply excerpt, and the message content. User-supplied content is escaped to neutralize XML-style delimiters and reduce prompt-injection risk.
3633
*
37-
* @param {Array} context - Historical messages fetched from Discord API
38-
* @param {Array} buffer - Buffered messages to evaluate
39-
* @returns {string} Formatted conversation text with section markers
34+
* @param {Array} context - Historical messages to include in <recent-history>.
35+
* @param {Array} buffer - Messages to include in <messages-to-evaluate>.
36+
* @returns {string} The formatted conversation text containing the assembled sections.
4037
*/
4138
export function buildConversationText(context, buffer) {
4239
const formatMsg = (m) => {
@@ -93,14 +90,16 @@ export function buildClassifyPrompt(context, snapshot, botUserId) {
9390
}
9491

9592
/**
96-
* Build the responder prompt from the template.
97-
* @param {Array} context - Historical context messages
98-
* @param {Array} snapshot - Buffer snapshot (messages to evaluate)
99-
* @param {Object} classification - Parsed classifier output
100-
* @param {Object} config - Bot configuration
101-
* @param {string} [memoryContext] - Memory context for target users
102-
* @returns {string} Interpolated respond prompt
103-
*/
93+
* Construct the responder prompt by combining conversation text, community rules, the system prompt, classification results, optional memory context, and search guardrails.
94+
* @param {Array} context - Historical context messages used to build conversation text.
95+
* @param {Array} snapshot - Buffer snapshot containing messages to evaluate.
96+
* @param {Object} classification - Classifier output containing decision details.
97+
* @param {string} classification.classification - The classification label.
98+
* @param {string} classification.reasoning - Explanatory reasoning for the classification.
99+
* @param {Array<string>} classification.targetMessageIds - IDs of messages targeted by the classification.
100+
* @param {Object} config - Bot configuration; `config.ai.systemPrompt` (if present) overrides the default system prompt.
101+
* @param {string} [memoryContext] - Optional serialized memory context to include for target users.
102+
* @returns {string} The fully interpolated responder prompt ready for the model. */
104103
export function buildRespondPrompt(context, snapshot, classification, config, memoryContext) {
105104
const conversationText = buildConversationText(context, snapshot);
106105
const communityRules = loadPrompt('community-rules');

src/modules/triage-respond.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ function logAssistantHistory(channelId, guildId, fallbackContent, sentMsg) {
4242
// ── Channel context fetching ─────────────────────────────────────────────────
4343

4444
/**
45-
* Fetch recent messages from Discord's API to provide conversation context
46-
* beyond the buffer window. Called at evaluation time (not accumulation) to
47-
* minimize API calls.
45+
* Retrieve recent channel messages to provide additional conversation context.
4846
*
49-
* @param {string} channelId - The channel to fetch history from
50-
* @param {import('discord.js').Client} client - Discord client
51-
* @param {Array} bufferSnapshot - Current buffer snapshot (to fetch messages before)
52-
* @param {number} [limit=15] - Maximum messages to fetch
53-
* @returns {Promise<Array>} Context messages in chronological order
47+
* Returns an array of context message objects in chronological order. Each object contains:
48+
* - `author`: display name (appended with " [BOT]" for bot accounts),
49+
* - `content`: sanitized message text truncated to the context character limit,
50+
* - `userId`, `messageId`, `timestamp`,
51+
* - `isContext`: true,
52+
* - `channelName`, `channelTopic`.
53+
*
54+
* @param {string} channelId - ID of the channel to fetch history from.
55+
* @param {Array} bufferSnapshot - Current buffer snapshot; messages are fetched before the oldest entry if present.
56+
* @param {number} [limit=15] - Maximum number of messages to fetch.
57+
* @returns {Array<Object>} Context message objects in chronological order.
5458
*/
5559
export async function fetchChannelContext(channelId, client, bufferSnapshot, limit = 15) {
5660
try {

web/src/components/dashboard/config-editor.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ import {
3030
import { DiscardChangesButton } from './reset-defaults-button';
3131

3232
/**
33-
* Generate a UUID with fallback for environments without crypto.randomUUID.
33+
* Generate a UUID string.
34+
*
35+
* Produces a v4-style UUID; in environments with native support this will use the platform API.
36+
*
37+
* @returns A v4 UUID string.
3438
*/
3539
function generateId(): string {
3640
if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
@@ -44,7 +48,12 @@ function generateId(): string {
4448
}
4549

4650
/**
47-
* Type guard that checks whether a value is a guild configuration object returned by the API.
51+
* Determines whether a value matches the expected GuildConfig shape returned by the API.
52+
*
53+
* Checks that `data` is a plain object that contains at least one known top-level config section
54+
* and that any present known sections are objects (not `null`, not primitives, and not arrays).
55+
*
56+
* @returns `true` if `data` appears to be a GuildConfig, `false` otherwise.
4857
*/
4958
function isGuildConfig(data: unknown): data is GuildConfig {
5059
if (typeof data !== 'object' || data === null || Array.isArray(data)) return false;
@@ -320,6 +329,13 @@ export function ConfigEditor() {
320329

321330
const failedSections: string[] = [];
322331

332+
/**
333+
* Applies a sequence of JSON Patch-like updates to the current guild's configuration via PATCH requests.
334+
*
335+
* @param sectionPatches - An ordered array of patch objects, each with a `path` (JSON pointer-like string) and `value` to send as the request body for a single PATCH operation.
336+
*
337+
* @throws Error - If the server responds with 401 (causes an abort and redirects to /login) or if any PATCH request returns a non-OK response; the error message contains the server-provided `error` field when available or the HTTP status.
338+
*/
323339
async function sendSection(sectionPatches: Array<{ path: string; value: unknown }>) {
324340
for (const patch of sectionPatches) {
325341
const res = await fetch(`/api/guilds/${encodeURIComponent(guildId)}/config`, {

web/src/components/dashboard/config-sections/ChallengesSection.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ const inputClasses =
1616
'w-full rounded-md border bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50';
1717

1818
/**
19-
* Daily Coding Challenges configuration section.
19+
* Render the Daily Coding Challenges configuration card.
2020
*
21-
* Provides controls for auto-posting daily coding challenges with hint and solve tracking.
21+
* Renders controls to enable/disable daily challenges and to edit channel ID, post time, and timezone.
22+
*
23+
* @param draftConfig - Current guild configuration draft containing `challenges` settings
24+
* @param saving - Whether configuration changes are being saved; when true inputs are disabled
25+
* @param onEnabledChange - Called with the new enabled state when the toggle is changed
26+
* @param onFieldChange - Called with a field name and value when an input changes (channelId is sent as `null` when empty)
27+
* @returns A React element containing the challenges configuration UI
2228
*/
2329
export function ChallengesSection({
2430
draftConfig,

web/src/components/dashboard/config-sections/CommunityFeaturesSection.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ const COMMUNITY_FEATURES = [
3535
] as const;
3636

3737
/**
38-
* Community Features configuration section.
38+
* Render the Community Features configuration card with a toggle for each feature.
3939
*
40-
* Provides toggles for enabling/disabling various community commands per guild.
40+
* Renders a titled card that lists community feature entries and a switch for enabling or disabling each feature for a guild.
41+
*
42+
* @param draftConfig - Guild draft configuration used to read each feature's `enabled` state (defaults to `false` when missing)
43+
* @param saving - When `true`, all toggles are disabled to prevent user interaction during save
44+
* @param onToggleChange - Callback invoked with the feature key and the new enabled state when a toggle changes
45+
* @returns The JSX element for the Community Features configuration section
4146
*/
4247
export function CommunityFeaturesSection({
4348
draftConfig,

web/src/components/dashboard/config-sections/GitHubSection.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ const inputClasses =
1616
'w-full rounded-md border bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50';
1717

1818
/**
19-
* GitHub Activity Feed configuration section.
19+
* Render the GitHub Activity Feed configuration panel inside a Card.
2020
*
21-
* Provides controls for GitHub feed channel and polling interval.
21+
* Renders controls to enable/disable the feed, set the feed channel ID, and configure the poll interval.
22+
*
23+
* @param draftConfig - Current guild configuration draft containing optional `github.feed` values.
24+
* @param saving - When true, disables all inputs to prevent interaction while saving.
25+
* @param onFieldChange - Callback invoked with (`field`, `value`) when a control changes.
26+
* @returns The JSX element for the GitHub Activity Feed configuration section.
2227
*/
2328
export function GitHubSection({ draftConfig, saving, onFieldChange }: GitHubSectionProps) {
2429
const feed = draftConfig.github?.feed ?? {};

web/src/components/dashboard/config-sections/MemorySection.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ const inputClasses =
1717
'w-full rounded-md border bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50';
1818

1919
/**
20-
* Memory configuration section.
20+
* Render the Memory configuration card for adjusting AI context memory and auto-extraction.
2121
*
22-
* Provides controls for AI context memory and auto-extraction settings.
22+
* @param draftConfig - Current draft guild configuration containing memory settings
23+
* @param saving - Whether a save operation is in progress; used to disable controls
24+
* @param onEnabledChange - Called with the new enabled state when the Memory toggle is changed
25+
* @param onFieldChange - Called with a field name and value when a configuration field is updated
26+
* @returns The Card element containing controls for Max Context Memories and Auto-Extract
2327
*/
2428
export function MemorySection({
2529
draftConfig,

web/src/components/dashboard/config-sections/PermissionsSection.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ const inputClasses =
1818
'w-full rounded-md border bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50';
1919

2020
/**
21-
* Permissions configuration section.
21+
* Render a permissions configuration card with an enable toggle, admin/moderator role selectors, and a bot owners input.
2222
*
23-
* Provides controls for role-based access and bot owner overrides.
23+
* The bot owners input shows a comma-separated list derived from the draft config and parses the input into an array of trimmed IDs on blur.
24+
*
25+
* @returns The rendered permissions configuration card element
2426
*/
2527
export function PermissionsSection({
2628
draftConfig,

web/src/components/dashboard/config-sections/StarboardSection.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ const inputClasses =
1616
'w-full rounded-md border bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50';
1717

1818
/**
19-
* Starboard configuration section.
19+
* Render the Starboard configuration card with controls to edit starboard settings.
2020
*
21-
* Provides controls for pinning popular messages to a starboard channel,
22-
* including threshold, emoji settings, and ignored channels.
21+
* @param draftConfig - Draft guild configuration whose `starboard` properties populate the form fields.
22+
* @param saving - When true, form controls are disabled to prevent edits while persisting changes.
23+
* @param onFieldChange - Callback invoked when a field changes; receives the field key (`'enabled' | 'channelId' | 'threshold' | 'emoji' | 'selfStarAllowed' | 'ignoredChannels'`) and the new value.
24+
* @returns A JSX element containing inputs and toggles for configuring the starboard feature.
2325
*/
2426
export function StarboardSection({ draftConfig, saving, onFieldChange }: StarboardSectionProps) {
2527
return (

web/src/components/dashboard/config-sections/TicketsSection.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,17 @@ const inputClasses =
1717
'w-full rounded-md border bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50';
1818

1919
/**
20-
* Tickets configuration section.
20+
* Renders the Tickets configuration card for a guild configuration.
2121
*
22-
* Provides controls for ticket system mode, support roles, auto-close settings,
23-
* and transcript channel configuration.
22+
* Displays an enable toggle and controls for ticket mode, support role ID,
23+
* category channel ID, auto-close hours, max open tickets per user, and
24+
* transcript channel ID. Inputs are disabled while `saving` is true and
25+
* changes are propagated via the provided callbacks.
26+
*
27+
* @param draftConfig - Current draft of the guild configuration
28+
* @param saving - Whether a save operation is in progress (disables inputs)
29+
* @param onEnabledChange - Called with the new enabled state when the toggle changes
30+
* @param onFieldChange - Called with field name and value when an input changes
2431
*/
2532
export function TicketsSection({
2633
draftConfig,

0 commit comments

Comments
 (0)