Compare commits
13
Commits
4f9d4a5c7d
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0792ff4dc0 | ||
|
|
eb89bb79ed | ||
|
|
7d6c741bb2 | ||
|
|
4cb4904517 | ||
|
|
4ee295bd29 | ||
|
|
65c9c2cd9e | ||
|
|
0a5254bf20 | ||
|
|
4a51f3055c | ||
|
|
185d81f0e0 | ||
|
|
ecbb538c9f | ||
|
|
4049ab4201 | ||
|
|
5d094829c4 | ||
|
|
abbd78f42b |
@@ -82,6 +82,19 @@ jobs:
|
||||
extra-conf: |
|
||||
sandbox = false
|
||||
accept-flake-config = true
|
||||
# Attic binary cache as substituter on the runner: lets CI pull the
|
||||
# prebuilt attic client (and any cached deps/builds) over HTTPS,
|
||||
# no SSH round-trip needed. extra-substituters (NOT
|
||||
# extra-trusted-substituters) is required — Determinate Nix never
|
||||
# merges trusted-* substituters for nix-store CLI clients.
|
||||
extra-substituters = https://attic.asepharyana.my.id/gmw
|
||||
extra-trusted-public-keys = gmw:Fq2Anzuhkb+T/hftWnPcveHSi21/RzIgIOeG8pCJa88=
|
||||
# NOTE: nix-installer-action unconditionally injects
|
||||
# 'build-provenance-tags' into /etc/nix/nix.conf (a Determinate
|
||||
# Nix-only setting). With determinate:false the runner's upstream
|
||||
# nix warns 'unknown setting build-provenance-tags' on every
|
||||
# invocation — benign, cosmetic. Switching determinate:true would
|
||||
# silence it but changes the runner's nix flavor.
|
||||
|
||||
- name: Cache Nix
|
||||
uses: DeterminateSystems/magic-nix-cache-action@v14
|
||||
@@ -107,13 +120,117 @@ jobs:
|
||||
ssh-keygen -y -f ~/.ssh/id_ed25519 >/dev/null 2>&1 || { echo "SSH key invalid"; exit 1; }
|
||||
ssh-keyscan -H "$VPS_HOST" >> ~/.ssh/known_hosts 2>/dev/null
|
||||
|
||||
# Push build result to Attic binary cache (attic.asepharyana.my.id) so
|
||||
# the VPS can substitute it instead of a single-stream `nix copy ssh://`.
|
||||
#
|
||||
# Fast path: push DIRECTLY from the runner to the public attic endpoint
|
||||
# (validated 2026-08-10: token auth over public HTTPS works without
|
||||
# Tailscale). This skips the ~794MB closure SSH copy to the VPS that
|
||||
# used to take 25+ minutes per new store path.
|
||||
#
|
||||
# The attic client is NOT in nixpkgs anymore and has no prebuilt
|
||||
# releases, so we pull the same prebuilt closure the VPS uses
|
||||
# (/nix/store/fygyy3yk4rqdknxkiwkqambpnhyax0k4-attic-0.1.0, ~52MB).
|
||||
# The closure itself lives in the attic cache (pushed once from the
|
||||
# VPS), so the runner bootstraps it over HTTPS via the configured
|
||||
# extra-substituters — no SSH round-trip. If that fails we fall back
|
||||
# to `nix copy --from ssh://`, then the old VPS-hop flow (SSH copy to
|
||||
# VPS, then attic push from the VPS over Tailscale) so the deploy step
|
||||
# always has a working closure path.
|
||||
- name: Push to Attic cache
|
||||
env:
|
||||
ATTIC_TOKEN: ${{ secrets.ATTIC_TOKEN }}
|
||||
run: |
|
||||
if [ -z "$ATTIC_TOKEN" ]; then
|
||||
echo "ATTIC_TOKEN not set; skipping attic push"
|
||||
exit 0
|
||||
fi
|
||||
STORE_PATH="${{ steps.build.outputs.store-path }}"
|
||||
ATTIC_DIR="/nix/store/fygyy3yk4rqdknxkiwkqambpnhyax0k4-attic-0.1.0"
|
||||
ATTIC_BIN="$ATTIC_DIR/bin/attic"
|
||||
|
||||
attic_push_vps_hop() {
|
||||
echo "Fallback: VPS-hop attic push"
|
||||
# Copy closure to VPS (fast if attic already has it via substitute)
|
||||
ssh "$VPS_USER@$VPS_HOST" "sudo /nix/var/nix/profiles/default/bin/nix-store --realise '$STORE_PATH'" 2>/dev/null \
|
||||
|| nix copy --to "ssh://$VPS_USER@$VPS_HOST" "$STORE_PATH"
|
||||
# Push from VPS → Attic over Tailscale.
|
||||
# --ignore-upstream-cache-filter is REQUIRED: without it, attic skips
|
||||
# writing the narinfo to gmw when chunks exist in the upstream
|
||||
# cache.nixos.org — leaving the path 404 on gmw so the VPS deploy's
|
||||
# nix-store --realise can't find it and falls back to ssh copy.
|
||||
# sudo: attic must read root's config (~/.config/attic), which has
|
||||
# the imrnes-ts server → Tailscale. Non-root users' configs only
|
||||
# have the public `pub` server → "Server imrnes-ts does not exist".
|
||||
ssh "$VPS_USER@$VPS_HOST" "sudo $ATTIC_BIN push imrnes-ts:gmw '$STORE_PATH' --jobs 4 --ignore-upstream-cache-filter" \
|
||||
|| echo "attic push failed (non-fatal; ssh copy fallback below)"
|
||||
}
|
||||
|
||||
# ── Get an attic client on the runner ────────────────────────────
|
||||
# Order: PATH → pull the prebuilt closure from the attic cache
|
||||
# itself (extra-substituters configured in Install Nix step, HTTPS
|
||||
# only, no SSH) → pull over ssh from the VPS → VPS-hop.
|
||||
# The attic client closure is stored in the attic cache (pushed
|
||||
# once from the VPS), so the fast path never depends on SSH.
|
||||
ATTIC_BIN=""
|
||||
if command -v attic >/dev/null 2>&1; then
|
||||
ATTIC_BIN="$(command -v attic)"
|
||||
elif nix-store --realise "$ATTIC_DIR" 2>/tmp/attic-bootstrap.err; then
|
||||
echo "✅ Pulled attic client from attic cache (HTTPS substituter)"
|
||||
ATTIC_BIN="$ATTIC_DIR/bin/attic"
|
||||
elif nix copy --from "ssh://$VPS_USER@$VPS_HOST" "$ATTIC_DIR" 2>>/tmp/attic-bootstrap.err; then
|
||||
echo "✅ Pulled attic client from VPS over ssh"
|
||||
ATTIC_BIN="$ATTIC_DIR/bin/attic"
|
||||
else
|
||||
echo "attic client unavailable on runner; using VPS-hop flow"
|
||||
echo "--- bootstrap errors (stderr) ---"
|
||||
tail -5 /tmp/attic-bootstrap.err 2>/dev/null || true
|
||||
attic_push_vps_hop
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# ── Direct push: runner → attic public endpoint ──────────────────
|
||||
# --ignore-upstream-cache-filter forces the narinfo write even when
|
||||
# the path's chunks already exist in upstream cache.nixos.org (which
|
||||
# attic would otherwise skip, leaving the path 404 on the gmw cache).
|
||||
mkdir -p "$HOME/.config/attic"
|
||||
cat > "$HOME/.config/attic/config.toml" <<EOF
|
||||
default-server = "pub"
|
||||
|
||||
[servers.pub]
|
||||
endpoint = "https://attic.asepharyana.my.id"
|
||||
token = "$ATTIC_TOKEN"
|
||||
EOF
|
||||
# Retry the direct push — a transient 502 (e.g. atticd restart,
|
||||
# Traefik blip) must not abort the whole closure upload. attic push
|
||||
# is idempotent, so re-running only uploads what's still missing.
|
||||
push_ok=""
|
||||
for attempt in 1 2 3; do
|
||||
if "$ATTIC_BIN" push pub:gmw "$STORE_PATH" --jobs 4 --ignore-upstream-cache-filter; then
|
||||
echo "✅ Pushed $STORE_PATH to attic directly from runner"
|
||||
push_ok=1
|
||||
break
|
||||
fi
|
||||
echo "⚠️ Direct attic push attempt $attempt/3 failed; retrying in 10s..."
|
||||
sleep 10
|
||||
done
|
||||
if [ -z "$push_ok" ]; then
|
||||
echo "Direct attic push failed after 3 attempts; using VPS-hop flow"
|
||||
attic_push_vps_hop
|
||||
fi
|
||||
|
||||
# NOTE: env files /etc/gmw/backend.env & /etc/gmw/discord-gateway.env are
|
||||
# managed MANUALLY on the VPS (source of truth). CI only builds & deploys.
|
||||
- name: Deploy ${{ matrix.service }} to VPS
|
||||
run: |
|
||||
STORE_PATH="${{ steps.build.outputs.store-path }}"
|
||||
echo "=== Copying ${{ matrix.service }}: $STORE_PATH ==="
|
||||
nix copy --to "ssh://$VPS_USER@$VPS_HOST" "$STORE_PATH"
|
||||
if [ -n "${{ secrets.ATTIC_TOKEN }}" ] && ssh "$VPS_USER@$VPS_HOST" "sudo /nix/var/nix/profiles/default/bin/nix-store --realise '$STORE_PATH'" 2>/dev/null; then
|
||||
echo "Substituted ${{ matrix.service }} from Attic cache"
|
||||
else
|
||||
echo "Attic substitute failed; falling back to ssh copy"
|
||||
nix copy --to "ssh://$VPS_USER@$VPS_HOST" "$STORE_PATH"
|
||||
fi
|
||||
|
||||
echo "=== Updating profile ==="
|
||||
ssh "$VPS_USER@$VPS_HOST" "sudo /nix/var/nix/profiles/default/bin/nix-env --profile /nix/var/nix/profiles/gmw-${{ matrix.service }} --set '$STORE_PATH'"
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ worktrees/
|
||||
.worktrees/
|
||||
services/frontend/frontend/dist/
|
||||
target/
|
||||
|
||||
nix/
|
||||
# Gitea CI runner logs
|
||||
.gitea/workflows/*.log
|
||||
|
||||
|
||||
@@ -59,6 +59,36 @@
|
||||
pnpm rebuild 2>&1 || true
|
||||
'';
|
||||
|
||||
# Shrink the shipped node_modules to production deps only. The full
|
||||
# install's .pnpm virtual store carries dev-only packages (biome,
|
||||
# typescript, esbuild, drizzle-kit, vitest, ... ~150MB+) that are never
|
||||
# needed at runtime, so we delete every .pnpm dir that is not part of
|
||||
# the resolved production graph (`pnpm list --prod`).
|
||||
#
|
||||
# NOTE: do NOT use `pnpm install --prod` here — it collapses the
|
||||
# public-hoist dir (.pnpm/node_modules) that runtime peer resolution
|
||||
# relies on (e.g. @lng2004/node-datachannel and @seydx/node-av-linux-x64
|
||||
# are only reachable through it), silently breaking voice/screenshare.
|
||||
# Instead we keep the full install's symlink layout and only prune
|
||||
# orphaned package dirs + broken symlinks.
|
||||
# Must run AFTER tsc (typescript is a devDep) and after native builds.
|
||||
pruneProd = ''
|
||||
echo "=== Pruning devDependencies (production-only node_modules) ==="
|
||||
pnpm list --prod --depth 999 --parseable 2>/dev/null \
|
||||
| grep -o '\.pnpm/[^/]*' | sort -u > $TMPDIR/prod-pnms.txt
|
||||
( cd node_modules/.pnpm \
|
||||
&& for d in */; do \
|
||||
d="''${d%/}"; \
|
||||
[ "$d" = "node_modules" ] && continue; \
|
||||
grep -qF ".pnpm/$d" $TMPDIR/prod-pnms.txt || rm -rf "$d"; \
|
||||
done ) || true
|
||||
# Drop symlinks whose .pnpm target was pruned (top-level, scoped dirs,
|
||||
# hoist, .bin — any depth). Mirrors stdenv's noBrokenSymlinks check,
|
||||
# which would otherwise fail the fixupPhase.
|
||||
find node_modules -type l ! -exec test -e {} \; -delete 2>/dev/null || true
|
||||
du -sh node_modules
|
||||
'';
|
||||
|
||||
# ---- Backend ----
|
||||
backend = pkgs.stdenv.mkDerivation {
|
||||
pname = "gmw-backend";
|
||||
@@ -97,7 +127,7 @@
|
||||
console.log('Fixed ' + count + ' files');
|
||||
"
|
||||
echo "=== Build complete ==="
|
||||
'';
|
||||
'' + pruneProd;
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/lib/gmw-backend
|
||||
@@ -170,6 +200,20 @@ WRAPPER
|
||||
fi
|
||||
fi
|
||||
done
|
||||
echo "=== Cleaning node-datachannel build tree ==="
|
||||
# Runtime only needs build/Release/node_datachannel.node + dist/ —
|
||||
# the cmake FetchContent sources (build/_deps, ~380MB), intermediate
|
||||
# cmake files, and the nested node_modules of build tooling (nw-gyp,
|
||||
# typescript, puppeteer, eslint, ... ~380MB) are build-time only.
|
||||
for pkg in node_modules/.pnpm/@lng2004+node-datachannel@*/node_modules/@lng2004/node-datachannel
|
||||
do
|
||||
if [ -d "$pkg" ]; then
|
||||
( cd "$pkg/build" \
|
||||
&& find . -mindepth 1 -maxdepth 1 ! -name 'Release' -exec rm -rf {} + ) 2>/dev/null || true
|
||||
rm -rf "$pkg/node_modules" 2>/dev/null || true
|
||||
echo "node-datachannel cleaned: $(du -sh "$pkg" | cut -f1)"
|
||||
fi
|
||||
done
|
||||
echo "=== Compiling TypeScript ==="
|
||||
npx tsc 2>&1
|
||||
echo "=== Fixing @/ path aliases to relative paths ==="
|
||||
@@ -198,7 +242,7 @@ WRAPPER
|
||||
console.log('Fixed ' + count + ' files');
|
||||
"
|
||||
echo "=== Build complete ==="
|
||||
'';
|
||||
'' + pruneProd;
|
||||
|
||||
installPhase = ''
|
||||
mkdir -p $out/lib/gmw-discord-gateway
|
||||
|
||||
@@ -3,6 +3,7 @@ allowBuilds:
|
||||
"@lng2004/node-datachannel": true
|
||||
esbuild: true
|
||||
node-av: true
|
||||
sharp: true
|
||||
zeromq: true
|
||||
# pnpm 11 requires build-script approvals here (the legacy `pnpm` field in
|
||||
# package.json is ignored). Native voice deps need their postinstall build.
|
||||
|
||||
@@ -21,7 +21,11 @@ import { config } from "../../shared/config/config.js";
|
||||
import { initializeDatabase } from "../../shared/database/drizzle.js";
|
||||
import { messageStore } from "../message-capture/messageStore.js";
|
||||
import type { MessageRecord } from "../message-capture/types.js";
|
||||
import { buildConversationContext } from "./conversationContext.js";
|
||||
import {
|
||||
buildConversationContext,
|
||||
buildLocationContext,
|
||||
} from "./conversationContext.js";
|
||||
import { buildConversationContextBlock } from "./moderationBuilders.js";
|
||||
import { runModerationAnalysis } from "./moderationOrchestrator.js";
|
||||
|
||||
const logger = createChildLogger("ai-analysis-worker");
|
||||
@@ -274,8 +278,14 @@ async function processBatch(job: {
|
||||
contextBefore,
|
||||
targets: messages,
|
||||
maxTokens: config.AI_ANALYSIS_MAX_CONTEXT_TOKENS,
|
||||
maxAgeMs: config.AI_ANALYSIS_CONTEXT_MAX_AGE_MS,
|
||||
gapMs: config.AI_ANALYSIS_CONTEXT_GAP_MS,
|
||||
});
|
||||
const contextBlock = buildConversationContextBlock({
|
||||
location: buildLocationContext(messages),
|
||||
descriptor: contextLines.descriptor,
|
||||
lines: contextLines.lines,
|
||||
});
|
||||
const contextText = contextLines.join("\n");
|
||||
|
||||
const targetIds = messages.map((m) => m.id);
|
||||
const contextIds = contextBefore.map((m) => m.id);
|
||||
@@ -289,7 +299,7 @@ async function processBatch(job: {
|
||||
// when media is present), not N per-message calls.
|
||||
const moderationResult = await runModerationAnalysis({
|
||||
targets: messages,
|
||||
contextText,
|
||||
contextBlock,
|
||||
attachments,
|
||||
});
|
||||
|
||||
@@ -359,8 +369,14 @@ async function processIndividual(job: {
|
||||
contextBefore,
|
||||
targets: [message],
|
||||
maxTokens: config.AI_ANALYSIS_MAX_CONTEXT_TOKENS,
|
||||
maxAgeMs: config.AI_ANALYSIS_CONTEXT_MAX_AGE_MS,
|
||||
gapMs: config.AI_ANALYSIS_CONTEXT_GAP_MS,
|
||||
});
|
||||
const contextBlock = buildConversationContextBlock({
|
||||
location: buildLocationContext([message]),
|
||||
descriptor: contextLines.descriptor,
|
||||
lines: contextLines.lines,
|
||||
});
|
||||
const contextText = contextLines.join("\n");
|
||||
|
||||
const contextIds = contextBefore.map((m) => m.id);
|
||||
const attachments = await messageStore.getAttachmentsForMessages([
|
||||
@@ -371,7 +387,7 @@ async function processIndividual(job: {
|
||||
try {
|
||||
const moderationResult = await runModerationAnalysis({
|
||||
targets: [message],
|
||||
contextText,
|
||||
contextBlock,
|
||||
attachments,
|
||||
});
|
||||
|
||||
|
||||
@@ -47,6 +47,40 @@ export function deriveRecommendedAction(msg: MessageRecord): string {
|
||||
return "none";
|
||||
}
|
||||
|
||||
/** Parse the flag list from a structured result or the stored column. */
|
||||
export function parseModerationFlags(
|
||||
message: MessageRecord,
|
||||
analysisResult?: AnalysisResult,
|
||||
): string[] {
|
||||
const flags = analysisResult?.flags ?? null;
|
||||
if (flags && flags.length > 0) return flags;
|
||||
const stored = message.ai_moderation_flags;
|
||||
if (!stored) return [];
|
||||
try {
|
||||
const parsed = JSON.parse(stored) as unknown;
|
||||
return Array.isArray(parsed)
|
||||
? parsed.filter((f): f is string => typeof f === "string")
|
||||
: [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the ONLY violation is the member's server nickname — the message
|
||||
* content itself is clean. Such messages must NOT be auto-deleted; the
|
||||
* correct enforcement is resetting the nickname to the default username.
|
||||
* Any other flag (sara, harassment, vulgar_language, ...) keeps the normal
|
||||
* delete path.
|
||||
*/
|
||||
export function isNicknameOnlyViolation(
|
||||
message: MessageRecord,
|
||||
analysisResult?: AnalysisResult,
|
||||
): boolean {
|
||||
const flags = parseModerationFlags(message, analysisResult);
|
||||
return flags.length > 0 && flags.every((f) => f === "offensive_username");
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a message qualifies for auto-deletion.
|
||||
* Uses the structured `analysisResult` fields when provided, falling back
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
import type { Client, PermissionString } from "discord.js-selfbot-v13";
|
||||
import { LRUCache } from "lru-cache";
|
||||
import { createChildLogger } from "@/shared/logger/index";
|
||||
import { config } from "../../shared/config/config.js";
|
||||
import { messageStore } from "../message-capture/messageStore.js";
|
||||
import type { MessageRecord } from "../message-capture/types.js";
|
||||
import { isEligibleForAutoDelete } from "./autoDeleteEligibility.js";
|
||||
import {
|
||||
isEligibleForAutoDelete,
|
||||
isNicknameOnlyViolation,
|
||||
} from "./autoDeleteEligibility.js";
|
||||
import { logDeletionToChannel } from "./autoDeleteLogger.js";
|
||||
import { sendDeletionNotification } from "./autoDeleteNotify.js";
|
||||
|
||||
@@ -15,6 +19,83 @@ export interface AutoDeleteResult {
|
||||
reason: string;
|
||||
}
|
||||
|
||||
// Cooldown per guild:user — a nick violation fires per message, but the
|
||||
// Discord PATCH is idempotent; hammering it on every message by the same
|
||||
// member is wasteful and risks rate limits.
|
||||
const recentNicknameResets = new LRUCache<string, number>({
|
||||
max: 200,
|
||||
ttl: config.AUTO_NICKNAME_RESET_COOLDOWN_MS ?? 10 * 60 * 1000,
|
||||
});
|
||||
|
||||
export function isNicknameResetInCooldown(
|
||||
guildId: string,
|
||||
userId: string,
|
||||
): boolean {
|
||||
return recentNicknameResets.has(`${guildId}:${userId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets a member's server nickname to the default (global username) —
|
||||
* Discord's `setNickname(null)` removes the custom nick so the member is
|
||||
* shown under their default username. Non-blocking; failures are logged
|
||||
* but never throw into the moderation pipeline.
|
||||
*/
|
||||
export async function resetOffensiveNickname(
|
||||
client: Client | undefined,
|
||||
guildId: string,
|
||||
userId: string,
|
||||
messageId: string,
|
||||
): Promise<boolean> {
|
||||
const cooldownKey = `${guildId}:${userId}`;
|
||||
try {
|
||||
if (!client?.user?.id) {
|
||||
logger.warn(
|
||||
{ messageId, guildId, userId },
|
||||
"Nick reset skipped: client missing",
|
||||
);
|
||||
return false;
|
||||
}
|
||||
if (userId === client.user.id) {
|
||||
logger.debug({ userId }, "Nick reset skipped: operator's own account");
|
||||
return false;
|
||||
}
|
||||
if (recentNicknameResets.has(cooldownKey)) {
|
||||
logger.debug({ guildId, userId }, "Nick reset skipped: cooldown active");
|
||||
return false;
|
||||
}
|
||||
if (config.AUTO_NICKNAME_RESET_ENABLED === false) return false;
|
||||
|
||||
const guild = client.guilds.cache.get(guildId);
|
||||
if (!guild) {
|
||||
logger.warn(
|
||||
{ messageId, guildId },
|
||||
"Nick reset skipped: guild not found",
|
||||
);
|
||||
return false;
|
||||
}
|
||||
const member = await guild.members.fetch(userId);
|
||||
// setNickname(null) = remove nickname → Discord shows global username
|
||||
await member.setNickname(null, "[auto] nickname melanggar aturan server");
|
||||
recentNicknameResets.set(cooldownKey, Date.now());
|
||||
logger.info(
|
||||
{ messageId, guildId, userId },
|
||||
"Offensive nickname reset to default username",
|
||||
);
|
||||
return true;
|
||||
} catch (error) {
|
||||
logger.warn(
|
||||
{
|
||||
messageId,
|
||||
guildId,
|
||||
userId,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
"Nick reset failed",
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Error Handling Utilities ────────────────────────────────────────
|
||||
|
||||
function getErrorCode(error: unknown): number | string | undefined {
|
||||
@@ -107,6 +188,57 @@ export async function attemptAutoDeleteFlaggedMessage(
|
||||
return { deleted: false, skipped: true, reason: "disabled" };
|
||||
}
|
||||
|
||||
// ── Nickname-only violation: reset nick, DO NOT delete ─────────────
|
||||
// When the only flag is offensive_username (message content is clean),
|
||||
// the problem is the server nickname, not the message. Enforcement is
|
||||
// removing the nickname back to the default username — the message stays.
|
||||
if (isNicknameOnlyViolation(message)) {
|
||||
if (
|
||||
!config.AUTO_DELETE_FLAGGED_DRY_RUN &&
|
||||
config.AUTO_NICKNAME_RESET_ENABLED !== false
|
||||
) {
|
||||
const inCooldown = isNicknameResetInCooldown(
|
||||
message.guild_id,
|
||||
message.user_id,
|
||||
);
|
||||
if (!inCooldown) {
|
||||
const resetOk = await resetOffensiveNickname(
|
||||
client,
|
||||
message.guild_id,
|
||||
message.user_id,
|
||||
message.id,
|
||||
);
|
||||
try {
|
||||
await messageStore.createModerationAction({
|
||||
message_id: message.id,
|
||||
user_id: message.user_id,
|
||||
guild_id: message.guild_id,
|
||||
action_type: "reset_nickname",
|
||||
reason:
|
||||
"nickname melanggar aturan server (offensive_username); pesan dibiarkan",
|
||||
executed_by: "auto-delete-manager",
|
||||
status: resetOk ? "executed" : "failed",
|
||||
error: resetOk ? null : "nickname_reset_failed",
|
||||
executed_at: resetOk ? Date.now() : null,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.warn(
|
||||
{
|
||||
messageId: message.id,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
"Failed to persist nickname reset action log",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.info(
|
||||
{ messageId: message.id, userId: message.user_id },
|
||||
"Nickname-only violation: message kept, nickname reset attempted",
|
||||
);
|
||||
return { deleted: false, skipped: true, reason: "nickname_only_violation" };
|
||||
}
|
||||
|
||||
// ── Status gate ──────────────────────────────────────────────────
|
||||
|
||||
if (message.ai_status !== "flagged" && message.ai_status !== "warn") {
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "../message-capture/messageMetadata.js";
|
||||
import type { MessageRecord } from "../message-capture/types.js";
|
||||
import { sanitizeDiscordTokens } from "./discordTokens.js";
|
||||
import { escapeXml, resolveDisplayName } from "./moderationBuilders.js";
|
||||
|
||||
const logger = createChildLogger("conversationContext");
|
||||
|
||||
@@ -13,6 +14,26 @@ export interface ConversationContextInput {
|
||||
contextBefore: MessageRecord[];
|
||||
targets: MessageRecord[];
|
||||
maxTokens: number;
|
||||
/**
|
||||
* Hard age cap for context messages (ms). Messages older than this
|
||||
* relative to the target are stale conversation noise and dropped.
|
||||
*/
|
||||
maxAgeMs?: number;
|
||||
/**
|
||||
* Silence threshold (ms). A gap between consecutive context messages
|
||||
* larger than this means the conversation restarted — older messages
|
||||
* belong to a previous conversation and are dropped.
|
||||
*/
|
||||
gapMs?: number;
|
||||
}
|
||||
|
||||
export interface ConversationContextResult {
|
||||
/** Formatted context lines (oldest → newest, recency-gated). */
|
||||
lines: string[];
|
||||
/** One-line flow descriptor: status, span, dropped counts. */
|
||||
descriptor: string;
|
||||
/** Number of context messages dropped by the recency gates. */
|
||||
dropped: number;
|
||||
}
|
||||
|
||||
let _encoder: ReturnType<typeof encodingForModel> | null = null;
|
||||
@@ -103,26 +124,143 @@ export function formatMessageForPrompt(
|
||||
msg: MessageRecord,
|
||||
label: "context" | "target",
|
||||
): string {
|
||||
const content = sanitizeDiscordTokens(
|
||||
renderDiscordMentions(msg.edited_content ?? msg.content, msg.metadata),
|
||||
const content = truncateContextLine(
|
||||
sanitizeDiscordTokens(
|
||||
renderDiscordMentions(msg.edited_content ?? msg.content, msg.metadata),
|
||||
),
|
||||
);
|
||||
const timestamp = formatTimestamp(msg.created_at);
|
||||
const mediaEvidence = formatMediaEvidenceForPrompt(msg.metadata);
|
||||
const mediaSuffix = mediaEvidence ? ` ${mediaEvidence}` : "";
|
||||
const refInfo = formatReferenceInfo(msg);
|
||||
return `[${label}] id=${msg.id} time=${timestamp} user=${msg.username}: ${content}${mediaSuffix}${refInfo}`;
|
||||
return `[${label}] id=${msg.id} time=${timestamp} user=${resolveDisplayName(msg)}: ${content}${mediaSuffix}${refInfo}`;
|
||||
}
|
||||
|
||||
/** Max content chars per context line — a single huge paste (log dump,
|
||||
* copypasta) must not eat the whole conversation budget. */
|
||||
const CONTEXT_LINE_CONTENT_MAX_CHARS = 1500;
|
||||
|
||||
/** Marker appended when a context line's content was cut. Distinct from the
|
||||
* target-content marker so the model knows which side was truncated. */
|
||||
export const CONTEXT_TRUNC_MARKER = "…[konteks dipotong: terlalu panjang]";
|
||||
|
||||
/** Cap one context message's content to CONTEXT_LINE_CONTENT_MAX_CHARS. */
|
||||
export function truncateContextLine(content: string): string {
|
||||
if (content.length <= CONTEXT_LINE_CONTENT_MAX_CHARS) return content;
|
||||
return `${content.slice(0, CONTEXT_LINE_CONTENT_MAX_CHARS).trimEnd()}${CONTEXT_TRUNC_MARKER}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a structured `<location_context .../>` element for the batch —
|
||||
* channel/thread name and age-restriction flags from captured message
|
||||
* metadata. The LLM uses it to judge messages in the right channel context
|
||||
* (e.g. a thread about a specific topic, or an age-restricted channel).
|
||||
* Returns "" when no channel metadata was captured.
|
||||
*/
|
||||
export function buildLocationContext(targets: MessageRecord[]): string {
|
||||
const target = targets[0];
|
||||
if (!target?.metadata) return "";
|
||||
try {
|
||||
const meta = JSON.parse(target.metadata) as {
|
||||
channel?: {
|
||||
channelName?: string | null;
|
||||
threadName?: string | null;
|
||||
topic?: string | null;
|
||||
nsfw?: boolean;
|
||||
ageRestricted?: boolean;
|
||||
nsfwLevel?: string | null;
|
||||
} | null;
|
||||
};
|
||||
const ch = meta?.channel;
|
||||
if (!ch) return "";
|
||||
const attrs: string[] = [`channel_id="${escapeXml(target.channel_id)}"`];
|
||||
if (ch.channelName)
|
||||
attrs.push(`channel_name="${escapeXml(ch.channelName)}"`);
|
||||
if (target.thread_id || ch.threadName) {
|
||||
if (target.thread_id)
|
||||
attrs.push(`thread_id="${escapeXml(target.thread_id)}"`);
|
||||
if (ch.threadName)
|
||||
attrs.push(`thread_name="${escapeXml(ch.threadName)}"`);
|
||||
}
|
||||
if (typeof ch.topic === "string" && ch.topic.trim().length > 0) {
|
||||
const topic =
|
||||
ch.topic.length > 200
|
||||
? `${ch.topic.slice(0, 200).trimEnd()}…`
|
||||
: ch.topic;
|
||||
attrs.push(`topic="${escapeXml(topic)}"`);
|
||||
}
|
||||
if (typeof ch.nsfw === "boolean") attrs.push(`nsfw="${ch.nsfw}"`);
|
||||
if (typeof ch.ageRestricted === "boolean") {
|
||||
attrs.push(`age_restricted="${ch.ageRestricted}"`);
|
||||
}
|
||||
return `<location_context ${attrs.join(" ")}/>`;
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds conversation historical context without including targets.
|
||||
* Calculates how much token budget targets use, and fills the rest with context.
|
||||
*
|
||||
* Two recency gates decide whether a conversation is STILL the same one
|
||||
* ("obrolan berlanjut") or already restarted:
|
||||
* - `gapMs`: a silence longer than this between two context messages cuts
|
||||
* the block there — earlier messages belong to a previous conversation.
|
||||
* - `maxAgeMs`: anything older than this relative to the target is noise.
|
||||
*
|
||||
* On a cold start (no recent context), the nearest messages are kept as a
|
||||
* sparse anchor and the descriptor says `cold_start` instead of `ongoing`,
|
||||
* so the LLM does not mistake scattered old messages for an active chat.
|
||||
*/
|
||||
export function buildConversationContext(
|
||||
input: ConversationContextInput,
|
||||
): string[] {
|
||||
): ConversationContextResult {
|
||||
const { contextBefore, targets, maxTokens } = input;
|
||||
const maxAgeMs = input.maxAgeMs ?? 45 * 60 * 1000;
|
||||
const gapMs = input.gapMs ?? 12 * 60 * 1000;
|
||||
|
||||
// Calculate tokens used by targets (parallel)
|
||||
const targetTime = targets.reduce(
|
||||
(min, t) => Math.min(min, t.created_at),
|
||||
targets[0]?.created_at ?? Date.now(),
|
||||
);
|
||||
|
||||
// ── Recency gating (walk newest → oldest) ───────────────────────────────
|
||||
const gated: MessageRecord[] = [];
|
||||
let latestSelected: MessageRecord | null = null;
|
||||
let gapBeforeMs: number | null = null;
|
||||
let dropped = 0;
|
||||
|
||||
for (let i = contextBefore.length - 1; i >= 0; i--) {
|
||||
const msg = contextBefore[i];
|
||||
// Age gate
|
||||
if (targetTime - msg.created_at > maxAgeMs) {
|
||||
dropped += i + 1; // everything older also exceeds the age cap
|
||||
break;
|
||||
}
|
||||
// Gap gate — silence between this message and the newer one already selected
|
||||
if (latestSelected && latestSelected.created_at - msg.created_at > gapMs) {
|
||||
gapBeforeMs = latestSelected.created_at - msg.created_at;
|
||||
dropped += i + 1;
|
||||
break;
|
||||
}
|
||||
gated.push(msg);
|
||||
latestSelected = msg;
|
||||
}
|
||||
|
||||
const gatedNewestFirst = gated.reverse();
|
||||
let status: "ongoing" | "cold_start" | "sparse";
|
||||
if (gatedNewestFirst.length === 0) {
|
||||
// Cold start — keep a small anchor of the nearest messages so the LLM
|
||||
// still senses the channel, but mark it clearly.
|
||||
status = "cold_start";
|
||||
gatedNewestFirst.push(...contextBefore.slice(-2)); // ± 2 nearest to target
|
||||
} else if (gapBeforeMs === null) {
|
||||
status = "ongoing";
|
||||
} else {
|
||||
status = "sparse";
|
||||
}
|
||||
|
||||
// ── Format + token budget (most recent first, like before) ─────────────
|
||||
const targetLines = targets.map((msg) =>
|
||||
formatMessageForPrompt(msg, "target"),
|
||||
);
|
||||
@@ -131,7 +269,7 @@ export function buildConversationContext(
|
||||
0,
|
||||
);
|
||||
|
||||
const contextLines = contextBefore.map((msg) =>
|
||||
const contextLines = gatedNewestFirst.map((msg) =>
|
||||
formatMessageForPrompt(msg, "context"),
|
||||
);
|
||||
const selectedContextLines: string[] = [];
|
||||
@@ -148,14 +286,26 @@ export function buildConversationContext(
|
||||
}
|
||||
}
|
||||
|
||||
const descriptorParts = [
|
||||
`[conversation_flow] status=${status}`,
|
||||
`context_msgs=${selectedContextLines.length}`,
|
||||
`dropped=${dropped}`,
|
||||
];
|
||||
if (gapBeforeMs !== null) {
|
||||
descriptorParts.push(`gap_before_min=${Math.round(gapBeforeMs / 60000)}`);
|
||||
}
|
||||
const descriptor = descriptorParts.join(" ");
|
||||
|
||||
logger.debug(
|
||||
{
|
||||
targetCount: targets.length,
|
||||
contextCount: selectedContextLines.length,
|
||||
status,
|
||||
dropped,
|
||||
usedTokens,
|
||||
maxTokens,
|
||||
},
|
||||
"Conversation context built",
|
||||
);
|
||||
return selectedContextLines;
|
||||
return { lines: selectedContextLines, descriptor, dropped };
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@ import { getChannelCulture } from "./channelCultureStore.js";
|
||||
import type { RetryState } from "./llmCaller.js";
|
||||
import { callModerationLLM } from "./llmCaller.js";
|
||||
import { prepareMediaMessage } from "./mediaAnalysisClient.js";
|
||||
import { buildUserProfilesBlock } from "./moderationBuilders.js";
|
||||
import { buildSystemPrompt as buildSystemPromptModular } from "./moderationPrompt.js";
|
||||
import { buildCorrectedFewShotExamples } from "./textBatchProcessor.js";
|
||||
import { getUserProfile } from "./userProfileStore.js";
|
||||
|
||||
const log = createChildLogger("mediaBatchProcessor");
|
||||
|
||||
@@ -26,7 +28,7 @@ const log = createChildLogger("mediaBatchProcessor");
|
||||
// ---------------------------------------------------------------------------
|
||||
export async function runMediaBatch(
|
||||
targets: MessageRecord[],
|
||||
contextText: string,
|
||||
contextBlock: string,
|
||||
attachments: AttachmentRecord[] | undefined,
|
||||
): Promise<{ results: AnalysisResult[]; raw: unknown }> {
|
||||
if (!targets.length) return { results: [], raw: null };
|
||||
@@ -58,14 +60,41 @@ export async function runMediaBatch(
|
||||
const channelCulture = channelCultureObj?.culture_summary;
|
||||
const correctedExamples = await buildCorrectedFewShotExamples();
|
||||
const systemText = buildSystemPromptModular({
|
||||
contextText,
|
||||
mode: "mixed",
|
||||
correctedExamples,
|
||||
channelCulture,
|
||||
});
|
||||
|
||||
// Gather user profiles ONCE for the whole batch and emit a deduplicated
|
||||
// <user_profiles> map (with last-generated timestamp); per-message blocks
|
||||
// (from prepareMediaMessage) reference it via <user_profile_ref>.
|
||||
const profileByUser = new Map<
|
||||
string,
|
||||
{
|
||||
text: string;
|
||||
asOf?: number | null;
|
||||
}
|
||||
>();
|
||||
for (const t of targets) {
|
||||
if (profileByUser.has(t.user_id)) continue;
|
||||
const profile = await getUserProfile(t.user_id);
|
||||
profileByUser.set(t.user_id, {
|
||||
text: profile?.profile_summary ?? "",
|
||||
asOf: profile?.last_analyzed_at ?? null,
|
||||
});
|
||||
}
|
||||
const userProfilesBlock = buildUserProfilesBlock(profileByUser);
|
||||
|
||||
const messagesBlock = prepared.map((p) => p.messageBlock).join("\n");
|
||||
const userContent = `<messages_to_analyze>\n${messagesBlock}\n</messages_to_analyze>`;
|
||||
// Data/instruction separation: the system prompt is stable per mode — all
|
||||
// per-batch context (profiles, conversation) lives in the USER payload,
|
||||
// ordered oldest-first so targets come last.
|
||||
const userBlocks = [
|
||||
userProfilesBlock?.trimEnd() ?? "",
|
||||
contextBlock?.trimEnd() ?? "",
|
||||
`<messages_to_analyze>\n${messagesBlock}\n</messages_to_analyze>`,
|
||||
].filter((b) => b.trim().length > 0);
|
||||
const userContent = userBlocks.join("\n\n");
|
||||
|
||||
const perMsgTimeout = config.AI_LLM_MEDIA_ANALYSIS_TIMEOUT_MS ?? 60000;
|
||||
const batchTimeout = Math.min(
|
||||
|
||||
@@ -494,8 +494,9 @@ export async function fetchUrlInline(
|
||||
sourceLabel: `[gambar dari URL ${url} (inline), pesan id=${targetId}]`,
|
||||
});
|
||||
} else if (result.type === "text" && result.textContent) {
|
||||
const titleAttr = result.title ? ` title="${escapeXml(result.title)}"` : "";
|
||||
webTexts.push(
|
||||
`<web_content url="${escapeXml(url)}">${escapeXml(result.textContent.slice(0, 2000))}</web_content>`,
|
||||
`<web_content url="${escapeXml(url)}"${titleAttr}>${escapeXml(result.textContent.slice(0, 2000))}</web_content>`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import { renderDiscordMentions } from "../message-capture/messageMetadata.js";
|
||||
import { messageStore } from "../message-capture/messageStore.js";
|
||||
import type { MessageRecord } from "../message-capture/types.js";
|
||||
import { sanitizeDiscordTokens } from "./discordTokens.js";
|
||||
import { sanitizeAiContent } from "./prompts/output.js";
|
||||
|
||||
/** Simple XML-escaping for content text. */
|
||||
export function escapeXml(s: string): string {
|
||||
@@ -19,6 +20,216 @@ export function escapeXml(s: string): string {
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Conversation context block — structured data for the USER message.
|
||||
//
|
||||
// All per-batch context lives in the USER message (not the SYSTEM prompt) so
|
||||
// the system prompt is stable per mode (cacheable on routers/providers) and
|
||||
// the role boundary is clean: instructions in SYSTEM, data in USER.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Outer char cap for the assembled `<conversation_context>` inner text. */
|
||||
export const CONVERSATION_CONTEXT_MAX_CHARS = 40_000;
|
||||
|
||||
/**
|
||||
* Wraps per-batch context data into structured XML blocks for the USER
|
||||
* message:
|
||||
*
|
||||
* <location_context channel_id="..." channel_name="..." nsfw="..."/>
|
||||
* <conversation_context>
|
||||
* [conversation_flow] status=ongoing context_msgs=12 dropped=0
|
||||
* [context] id=... time=... user=...: isi pesan
|
||||
* ...
|
||||
* </conversation_context>
|
||||
*
|
||||
* Empty blocks are omitted entirely (never emit a hollow `<conversation_context>`
|
||||
* with no content). The inner text is AI/user-derived and passed through
|
||||
* `sanitizeAiContent` (CDATA + XML-escape) to block prompt injection.
|
||||
*/
|
||||
export function buildConversationContextBlock(input: {
|
||||
/** Pre-built `<location_context .../>` string (or ""). */
|
||||
location?: string;
|
||||
/** `[conversation_flow]` descriptor line from buildConversationContext. */
|
||||
descriptor?: string;
|
||||
/** `[context]` lines, oldest → newest. */
|
||||
lines: string[];
|
||||
}): string {
|
||||
const blocks: string[] = [];
|
||||
const location = input.location?.trim();
|
||||
if (location) blocks.push(location);
|
||||
|
||||
const inner = [input.descriptor ?? "", ...input.lines]
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.join("\n");
|
||||
if (inner) {
|
||||
blocks.push(
|
||||
`<conversation_context>\n${sanitizeAiContent(inner, CONVERSATION_CONTEXT_MAX_CHARS)}\n</conversation_context>`,
|
||||
);
|
||||
}
|
||||
return blocks.join("\n");
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per-message content bounds — protects the LLM token budget from a single
|
||||
// huge paste (stack traces, log dumps, copypasta). Truncation is explicit so
|
||||
// the model never mistakes the cut for a real message boundary.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Max characters of a message's content sent to the LLM `<content>` payload. */
|
||||
export const AI_CONTENT_MAX_CHARS = 4000;
|
||||
|
||||
/** Marker appended when a message is longer than AI_CONTENT_MAX_CHARS. */
|
||||
export const AI_CONTENT_TRUNC_MARKER = "\n…[pesan dipotong: terlalu panjang]";
|
||||
|
||||
/** Truncate a message's content for the LLM `<content>` payload. */
|
||||
export function truncateForAi(content: string): string {
|
||||
if (content.length <= AI_CONTENT_MAX_CHARS) return content;
|
||||
return `${content.slice(0, AI_CONTENT_MAX_CHARS)}${AI_CONTENT_TRUNC_MARKER}`;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// User profile deduplication — a batch can contain many messages from the
|
||||
// same user. Instead of repeating the (up to 3000-char) profile summary on
|
||||
// every message, emit a single <user_profiles> map per batch and reference
|
||||
// entries per message with <user_profile_ref user_id="..."/>.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface UserProfileEntry {
|
||||
/** Profile summary text (from user_profiles.profile_summary). */
|
||||
text: string;
|
||||
/** Epoch ms when the profile was last generated — staleness signal for
|
||||
* the LLM (a profile from months ago may not reflect current behavior). */
|
||||
asOf?: number | null;
|
||||
}
|
||||
|
||||
/** Build a deduplicated `<user_profiles>` map block, keyed by Discord user id. */
|
||||
export function buildUserProfilesBlock(
|
||||
profiles: ReadonlyMap<string, UserProfileEntry>,
|
||||
): string {
|
||||
const entries = Array.from(profiles.entries()).filter(
|
||||
([, entry]) => entry.text.trim().length > 0,
|
||||
);
|
||||
if (entries.length === 0) return "";
|
||||
const lines = entries.map(([userId, entry]) => {
|
||||
const asOfAttr =
|
||||
typeof entry.asOf === "number" && entry.asOf > 0
|
||||
? ` as_of="${new Date(entry.asOf).toISOString()}"`
|
||||
: "";
|
||||
return ` <user_profile user_id="${escapeXml(userId)}"${asOfAttr}>${sanitizeAiContent(entry.text)}</user_profile>`;
|
||||
});
|
||||
return `<user_profiles>\n${lines.join("\n")}\n</user_profiles>`;
|
||||
}
|
||||
|
||||
/** Per-message reference tag pointing at an entry in the `<user_profiles>` map. */
|
||||
export function buildUserProfileRef(userId: string): string {
|
||||
return `<user_profile_ref user_id="${escapeXml(userId)}"/>`;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// User reputation — richer than a bare trust score.
|
||||
//
|
||||
// The trust model tracks total_infractions, a clean-message streak and the
|
||||
// last infraction timestamp. Feeding all of it to the LLM lets it tell a
|
||||
// first-timer (same score, 1 infraction) from a repeat offender (score 50,
|
||||
// 3 infractions, last one yesterday) — the same score means very different
|
||||
// things in those two contexts.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface ReputationAttrsSource {
|
||||
trust_score: number;
|
||||
total_infractions: number;
|
||||
clean_message_streak: number;
|
||||
last_infraction_at: number | null;
|
||||
}
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
const REPEAT_OFFENSE_WINDOW_MS = 7 * DAY_MS;
|
||||
|
||||
/**
|
||||
* Formats reputation fields into XML attributes for `<user_reputation .../>`.
|
||||
* Derived signals: last_offense_days_ago (0 = today) and repeat_offender
|
||||
* (infraction within the last 7 days) are computed here so both the text and
|
||||
* media paths emit the exact same shape.
|
||||
*/
|
||||
export function formatReputationAttrs(
|
||||
rep: ReputationAttrsSource,
|
||||
now: number = Date.now(),
|
||||
): string {
|
||||
const attrs = [
|
||||
`trust_score="${rep.trust_score}"`,
|
||||
`total_infractions="${rep.total_infractions}"`,
|
||||
`clean_streak="${rep.clean_message_streak}"`,
|
||||
];
|
||||
if (
|
||||
typeof rep.last_infraction_at === "number" &&
|
||||
rep.last_infraction_at > 0
|
||||
) {
|
||||
const daysAgo = Math.max(
|
||||
0,
|
||||
Math.floor((now - rep.last_infraction_at) / DAY_MS),
|
||||
);
|
||||
attrs.push(`last_offense_days_ago="${daysAgo}"`);
|
||||
const isRepeat =
|
||||
rep.total_infractions > 0 &&
|
||||
now - rep.last_infraction_at <= REPEAT_OFFENSE_WINDOW_MS;
|
||||
if (isRepeat) attrs.push(`repeat_offender="true"`);
|
||||
}
|
||||
return attrs.join(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an optional `<user_history>` block (last flagged messages) from
|
||||
* getUserRecentInfractions rows. Only emitted when there is real history —
|
||||
* lets the LLM see the PATTERN (e.g. the same scam link posted repeatedly)
|
||||
* without treating old flags as proof for the current message.
|
||||
*/
|
||||
export function buildUserHistoryXml(
|
||||
history: Array<{
|
||||
content: string;
|
||||
severity: string | null;
|
||||
created_at: number;
|
||||
}>,
|
||||
now: number = Date.now(),
|
||||
): string {
|
||||
const filtered = history.filter((h) => h.content?.trim());
|
||||
if (filtered.length === 0) return "";
|
||||
const lines = filtered.map((h) => {
|
||||
const daysAgo = Math.max(0, Math.floor((now - h.created_at) / DAY_MS));
|
||||
const severityAttr = h.severity
|
||||
? ` severity="${escapeXml(h.severity)}"`
|
||||
: "";
|
||||
const snippet =
|
||||
h.content.length > 100
|
||||
? `${h.content.slice(0, 100).trimEnd()}…`
|
||||
: h.content;
|
||||
return ` <infraction${severityAttr} time_ago_days="${daysAgo}">${escapeXml(snippet)}</infraction>`;
|
||||
});
|
||||
return `<user_history>\n${lines.join("\n")}\n</user_history>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the message author was a bot (captured in metadata.author.bot).
|
||||
* Bot posts (logging bots, webhook-style automation) deserve different
|
||||
* scrutiny than user posts — expose the flag instead of hiding it.
|
||||
*/
|
||||
export function resolveIsBot(msg: MessageRecord): boolean {
|
||||
if (!msg.metadata) return false;
|
||||
try {
|
||||
const meta = JSON.parse(msg.metadata) as {
|
||||
author?: { bot?: boolean } | null;
|
||||
};
|
||||
return Boolean(meta?.author?.bot);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether the shown content is an EDIT of the original post (evasion signal). */
|
||||
export function resolveIsEdited(msg: MessageRecord): boolean {
|
||||
return Boolean(msg.edited_content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the real text content for AI analysis, stripping fallback text
|
||||
* that getDisplayContent() synthesized ("[Attachment: ...]", "[Sticker: ...]",
|
||||
@@ -36,6 +247,27 @@ export function getAnalysisContent(message: MessageRecord): string {
|
||||
).trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Server nickname (member.displayName) when captured, else the author
|
||||
* username. Discord shows the server nickname to other members, so the LLM
|
||||
* should see the same name the channel sees — and a nickname can carry
|
||||
* moderation signal itself (offensive nick + clean message → low warn).
|
||||
*/
|
||||
export function resolveDisplayName(msg: MessageRecord): string {
|
||||
if (msg.metadata) {
|
||||
try {
|
||||
const meta = JSON.parse(msg.metadata) as {
|
||||
member?: { displayName?: string | null } | null;
|
||||
};
|
||||
const dn = meta?.member?.displayName;
|
||||
if (dn && dn.trim().length > 0) return dn;
|
||||
} catch {
|
||||
// malformed metadata — fall back to username
|
||||
}
|
||||
}
|
||||
return msg.username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a <reference> XML element for reply/forward/crosspost context.
|
||||
*/
|
||||
|
||||
@@ -35,7 +35,13 @@ const log = createChildLogger("moderationOrchestrator");
|
||||
// ---------------------------------------------------------------------------
|
||||
export interface ModerationInput {
|
||||
targets: MessageRecord[];
|
||||
contextText: string;
|
||||
/**
|
||||
* Pre-built XML context block for the USER message (from
|
||||
* `buildConversationContextBlock`): `<location_context .../>` +
|
||||
* `<conversation_context>...</conversation_context>`. Kept out of the
|
||||
* system prompt so it stays stable/cacheable per mode.
|
||||
*/
|
||||
contextBlock: string;
|
||||
attachments?: AttachmentRecord[];
|
||||
}
|
||||
|
||||
@@ -62,7 +68,7 @@ export interface ModerationOutput {
|
||||
export async function runModerationAnalysis(
|
||||
input: ModerationInput,
|
||||
): Promise<ModerationOutput> {
|
||||
const { targets, contextText, attachments } = input;
|
||||
const { targets, contextBlock, attachments } = input;
|
||||
|
||||
initSearxngCache(config.REDIS_URL);
|
||||
if (!targets.length) throw new Error("No targets provided for analysis");
|
||||
@@ -320,10 +326,10 @@ export async function runModerationAnalysis(
|
||||
// Run both paths in parallel
|
||||
const [textBatchResult, mediaBatchResult] = await Promise.all([
|
||||
textOnlyTargets.length > 0
|
||||
? runTextOnlyBatch(textOnlyTargets, contextText)
|
||||
? runTextOnlyBatch(textOnlyTargets, contextBlock)
|
||||
: Promise.resolve({ results: [] as AnalysisResult[], raw: null }),
|
||||
mediaTargets.length > 0
|
||||
? runMediaBatch(mediaTargets, contextText, attachments)
|
||||
? runMediaBatch(mediaTargets, contextBlock, attachments)
|
||||
: Promise.resolve({ results: [] as AnalysisResult[], raw: null }),
|
||||
]);
|
||||
|
||||
|
||||
@@ -31,11 +31,16 @@ Struktur wajib:
|
||||
]
|
||||
}
|
||||
|
||||
Instruksi per field:
|
||||
- "message_id": WAJIB sama persis dengan id di input. Setiap <message> di <messages_to_analyze> menghasilkan SATU hasil. Jangan gabungkan beberapa pesan, jangan lewati, jangan karang id.
|
||||
- "evidence": kutipan PERSIS frasa yang melanggar (maks 1 baris). Pelanggaran di gambar/sticker → kutip deskripsi Media analysis. Pelanggaran lewat balasan/referensi → sebut konteks pesan yang dibalas. Boleh tambah label sumber, mis. [media analysis] / [web_search] / [reply]. Kosong jika clean.
|
||||
|
||||
## PERSONALITY & MEMORI — Profil Pengguna dan Kultur Channel
|
||||
Data konteks tersedia: <user_profile> (ringkasan kepribadian pengguna) dan <channel_culture> (topik/vibe channel).
|
||||
Data konteks tersedia: <user_profiles> (peta ringkasan kepribadian, di pesan USER), <user_reputation> (skor trust), dan <channel_culture> (topik/vibe channel). Setiap <message> dapat memuat <user_profile_ref user_id="..."/> yang menunjuk ke entri di peta <user_profiles>.
|
||||
Gunakan untuk personalisasi analysis, tapi:
|
||||
- Profil adalah KONTEKS, bukan bukti. Profil mencurigakan ≠ flag; profil bersih ≠ loloskan pelanggaran.
|
||||
- Perubahan perilaku mencolok (biasanya teknis tiba-tiba provokatif) layak dicatat di analysis.
|
||||
- <user_history> (kutipan pesan yang pernah di-flag) = pola pelanggaran lama. Gunakan untuk mendeteksi PENGULANGAN (mis. spam link yang sama, provokasi berulang), tapi JANGAN memflag pesan bersih hanya karena riwayat.
|
||||
- JANGAN paksa referensi profil jika tidak relevan — analysis natural lebih baik.
|
||||
- Channel culture coding/teknis → pesan teknis lebih wajar; channel santai → slang lebih wajar. Jangan dipakai mengabaikan pelanggaran nyata.
|
||||
|
||||
@@ -54,6 +59,7 @@ Contoh buruk: "Pesan berisi teks dan gambar tanpa pelanggaran." (mengabaikan buk
|
||||
- **conflict_instigation:** "Pengirim <ajakan memicu konflik>. <konteks>. Diberi peringatan karena berpotensi memicu drama."
|
||||
- **Username ofensif (pesan bersih):** "Pengirim memiliki username yang <alasan ofensif>. Isi pesan hanya <isi>. Diberi warning ringan." — (pesan memperkuat): "<username SARA> + isi pesan memperkuat tone kebencian. Pelanggaran berat."
|
||||
- **Evasi (zalgo/leetspeak):** "Pengirim menggunakan teknik obfuscation untuk menyembunyikan <makna asli>. <dampak>. <kesimpulan>."
|
||||
- **Spam (repetitions > 1):** "Pengirim mengirim teks yang sama sebanyak N kali dalam waktu singkat. <isi pesan>. Diberi peringatan karena spam berulang." — nilai tetap dari isi; pengulangan saja (mis. "ok" x5 dalam obrolan aktif) bukan pelanggaran.
|
||||
- **sexual_deviation:** "Pengirim <konten penyimpangan>. <konteks>. Melanggar kebijakan server."
|
||||
- **SARA/penistaan agama:** "Pengirim <jenis penistaan spesifik: parodi ayat, mengaku Tuhan, mockery ritual, istilah agama sebagai joke, provokasi antar-agama>. <bukti>. Melanggar kebijakan SARA." — JANGAN gunakan kata "bercanda" untuk SARA.
|
||||
|
||||
@@ -66,7 +72,7 @@ CRITICAL:
|
||||
- Jika pesan adalah BALASAN (reply) ke pesan lain, jelaskan konteks balasannya: apa yang sedang dibicarakan, siapa yang dibalas (tanpa nama, cukup peran/isi pesan yang dibalas), dan bagaimana tanggapan pengirim terhadapnya.
|
||||
- Gunakan informasi dari Media analysis untuk mendeskripsikan gambar.
|
||||
- Analisis harus MEMBERI KONTEKS, bukan hanya menyatakan status.
|
||||
- GUNAKAN <user_profile> untuk personalisasi analysis — jadikan analysis terasa seperti sistem "mengenal" pengguna.
|
||||
- GUNAKAN <user_profile_ref>/<user_profiles> untuk personalisasi analysis — jadikan analysis terasa seperti sistem "mengenal" pengguna.
|
||||
- Jika perilaku pesan menyimpang dari profil yang diketahui, CATAT dalam analysis sebagai informasi kontekstual yang relevan.
|
||||
- JANGAN paksa referensi profil jika tidak relevan — analysis natural lebih baik dari yang dipaksakan.`;
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@ Gambar/sticker/embed/preview link sudah DIDESKRIPSIKAN vision model sebelum batc
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export interface BuildSystemPromptOptions {
|
||||
contextText: string;
|
||||
/** Prompt mode — determines which sections are included. */
|
||||
mode: PromptMode;
|
||||
/** @deprecated Use `mode` instead. */
|
||||
@@ -59,7 +58,6 @@ export interface BuildSystemPromptOptions {
|
||||
|
||||
export function buildSystemPrompt(options: BuildSystemPromptOptions): string {
|
||||
const {
|
||||
contextText,
|
||||
mode,
|
||||
includeMediaInstructions,
|
||||
correction,
|
||||
@@ -105,15 +103,39 @@ export function buildSystemPrompt(options: BuildSystemPromptOptions): string {
|
||||
}
|
||||
|
||||
parts.push(
|
||||
`## Konteks Pengguna\nSetiap pesan mungkin memiliki tag <user_reputation>. Tag ini hanya indikator **referensi**, bukan bukti pelanggaran. Nilai trust_score yang rendah bukan alasan untuk memflag pesan yang bersih. Nilai trust_score yang tinggi bukan alasan untuk mengabaikan pelanggaran nyata. **Setiap pesan harus dinilai berdasarkan isinya sendiri.**`,
|
||||
`## Blok Data di Pesan USER\n` +
|
||||
`Semua data dinamis per-batch dikirim di pesan USER — system prompt ini TIDAK memuat data batch:\n` +
|
||||
`- <location_context .../> = metadata channel/thread (channel_id, channel_name, thread_name, topic, nsfw, age_restricted). topic = deskripsi resmi channel — pakai untuk menilai kesesuaian pesan dengan tujuan channel.\n` +
|
||||
`- <conversation_context> = obrolan SEBELUM pesan target. Baris "[context]" di dalamnya BUKAN yang dinilai.\n` +
|
||||
`- <user_profiles> = peta ringkasan kepribadian per user_id (attr as_of = kapan profil terakhir dibuat — profil lama mungkin tidak mencerminkan perilaku terkini); setiap <message> merujuk lewat <user_profile_ref user_id="..."/>.\n` +
|
||||
`- <web_searches> / <web_content> = bukti web (lihat "Web Sebagai Bukti Utama").\n` +
|
||||
`- <messages_to_analyze> = pesan-pesan TARGET yang WAJIB dinilai. Atribut <message>: id, user (nama server), time (ISO — kapan pesan dikirim), repetitions (N = teks pendek sama muncul N kali di batch — sinyal spam), bot (true jika dari bot), edited (true jika konten adalah hasil edit setelah posting).`,
|
||||
);
|
||||
|
||||
parts.push(
|
||||
`## Konteks Pengguna (Referensi, Bukan Bukti)\n` +
|
||||
`Konteks per pengguna hanya indikator **referensi** untuk personalisasi analisis, BUKAN bukti pelanggaran:\n` +
|
||||
`- <user_reputation trust_score="..." total_infractions="..." clean_streak="..." last_offense_days_ago="..." repeat_offender="..."> = histori moderasi pengguna. Skor rendah BUKAN alasan memflag pesan bersih; skor tinggi BUKAN alasan mengabaikan pelanggaran nyata. repeat_offender="true" = ada pelanggaran dalam 7 hari terakhir.\n` +
|
||||
`- <user_history> (di dalam <user_reputation>) = kutipan pesan-pesan pengguna yang PERNAH di-flag. Gunakan untuk mengenali POLA berulang (spam link sama, provokasi), tapi JANGAN memflag pesan bersih hanya karena riwayat.\n` +
|
||||
`- <user_profiles> (di pesan USER) = peta ringkasan kepribadian per user_id. <user_profile_ref user_id="..."/> dalam sebuah pesan menunjuk ke peta itu. Tanpa ref = tidak ada profil untuk pengguna tersebut.\n` +
|
||||
`- Profil berguna untuk mengenali penyimpangan perilaku mencolok (mis. pengguna teknis tiba-tiba provokatif), tapi JANGAN memflag atau meloloskan hanya karena profil.\n` +
|
||||
`**Setiap pesan dinilai berdasarkan isinya sendiri.**`,
|
||||
);
|
||||
|
||||
parts.push(
|
||||
`## Framing: Konteks vs Target\n` +
|
||||
`- Baris dalam <conversation_context> berformat "[context] id=... time=<ISO> user=<nama>: isi", diurutkan paling lama → paling baru. Baris pertama biasanya "[conversation_flow] status=... context_msgs=... dropped=..." — metadata sistem tentang status percakapan (ongoing/sparse/cold_start), BUKAN pesan yang dinilai.\n` +
|
||||
`- <messages_to_analyze> berisi pesan-pesan TARGET yang WAJIB dinilai. Hasilkan SATU hasil per message_id — jangan menggabungkan beberapa pesan, jangan melewati, jangan mengarang id.\n` +
|
||||
`- Setiap target dinilai berdasarkan isinya sendiri; konteks percakapan memengaruhi interpretasi, bukan menggantikan isi pesan.\n` +
|
||||
`- Marker "…[pesan dipotong: terlalu panjang]" = konten TARGET sengaja dipotong; marker "…[konteks dipotong: terlalu panjang]" = konten pesan KONTEKS dipotong. Nilai dari bagian yang terlihat; pemotongan BUKAN pelanggaran dan BUKAN teknik evasi.\n` +
|
||||
`- Atribut time= pada <message> target = kapan pesan dikirim (ISO). Pakai untuk menilai kerelevanan waktu (mis. pesan lama di-bump, spam beruntun dalam menit yang sama).\n` +
|
||||
`- repetitions="N" pada <message> = teks pendek yang sama muncul N kali dalam batch — pertimbangkan sebagai sinyal spam, tapi nilai tetap dari isi pesan.\n` +
|
||||
`- bot="true" = pengirim adalah bot (otomatisasi), bukan pengguna manusia — jangan perlakukan sebagai pelanggaran personal, tapi kontennya tetap dinilai.\n` +
|
||||
`- edited="true" = konten yang ditampilkan adalah hasil edit setelah posting (sinyal potensi evasi), nilai konten saat ini apa adanya.`,
|
||||
);
|
||||
|
||||
parts.push(OUTPUT_INSTRUCTIONS);
|
||||
|
||||
// XML-delimited context — prevents prompt injection
|
||||
const delimitedContext = `<conversation_context>\n${sanitizeAiContent(contextText, 8000)}\n</conversation_context>`;
|
||||
parts.push(delimitedContext);
|
||||
|
||||
let base = parts.join("\n\n");
|
||||
|
||||
if (correction) {
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
* the LLM for analysis. Extracted from moderationOrchestrator.ts.
|
||||
*/
|
||||
import { createChildLogger } from "@/shared/logger/index";
|
||||
import { delay } from "@/shared/utils/index";
|
||||
import { config } from "../../shared/config/config.js";
|
||||
import { resizeImageForVision } from "../attachment-upload/imageResizer.js";
|
||||
import type {
|
||||
AnalysisResult,
|
||||
MessageRecord,
|
||||
@@ -14,15 +16,21 @@ import type {
|
||||
import { getChannelCulture } from "./channelCultureStore.js";
|
||||
import type { ModerationPromptContent, RetryState } from "./llmCaller.js";
|
||||
import { callModerationLLM } from "./llmCaller.js";
|
||||
import { analyzeSingleMediaImage } from "./mediaAnalysisClient.js";
|
||||
import {
|
||||
buildReferenceXml,
|
||||
buildUserHistoryXml,
|
||||
buildUserProfileRef,
|
||||
buildUserProfilesBlock,
|
||||
escapeXml,
|
||||
formatReputationAttrs,
|
||||
getAnalysisContent,
|
||||
resolveDisplayName,
|
||||
resolveIsBot,
|
||||
resolveIsEdited,
|
||||
truncateForAi,
|
||||
} from "./moderationBuilders.js";
|
||||
import {
|
||||
buildSystemPrompt as buildSystemPromptModular,
|
||||
sanitizeAiContent,
|
||||
} from "./moderationPrompt.js";
|
||||
import { buildSystemPrompt as buildSystemPromptModular } from "./moderationPrompt.js";
|
||||
import { logModerationAnalysis } from "./responseLogger.js";
|
||||
import {
|
||||
extractSearchQueries,
|
||||
@@ -32,7 +40,11 @@ import {
|
||||
import { getRecentCorrectedModerations } from "./textCacheStore.js";
|
||||
import { extractUrlsFromText, fetchUrlSafely } from "./urlFetcher.js";
|
||||
import { getUserProfile } from "./userProfileStore.js";
|
||||
import { initializeUserReputation } from "./userReputationStore.js";
|
||||
import {
|
||||
getUserRecentInfractions,
|
||||
initializeUserReputation,
|
||||
} from "./userReputationStore.js";
|
||||
import type { MessageImagePart } from "./visionAnalyzer.js";
|
||||
|
||||
const log = createChildLogger("textBatchProcessor");
|
||||
|
||||
@@ -69,7 +81,7 @@ export async function buildCorrectedFewShotExamples(): Promise<string> {
|
||||
// ---------------------------------------------------------------------------
|
||||
export async function runTextOnlyBatch(
|
||||
targets: MessageRecord[],
|
||||
contextText: string,
|
||||
contextBlock: string,
|
||||
): Promise<{ results: AnalysisResult[]; raw: unknown }> {
|
||||
if (!targets.length) return { results: [], raw: null };
|
||||
|
||||
@@ -84,22 +96,33 @@ export async function runTextOnlyBatch(
|
||||
allUrls.add(url);
|
||||
}
|
||||
const urlArr = Array.from(allUrls).slice(0, 10);
|
||||
if (urlArr.length === 0) return new Map<string, string>();
|
||||
if (urlArr.length === 0) {
|
||||
return {
|
||||
text: new Map<string, string>(),
|
||||
image: new Map<string, { data: Buffer; mimeType: string }>(),
|
||||
title: new Map<string, string>(),
|
||||
};
|
||||
}
|
||||
const results = await Promise.allSettled(
|
||||
urlArr.map((url) => fetchUrlSafely(url)),
|
||||
);
|
||||
const map = new Map<string, string>();
|
||||
const textMap = new Map<string, string>();
|
||||
const imageMap = new Map<string, { data: Buffer; mimeType: string }>();
|
||||
const titleMap = new Map<string, string>();
|
||||
for (let i = 0; i < urlArr.length; i++) {
|
||||
const r = results[i];
|
||||
if (
|
||||
r.status === "fulfilled" &&
|
||||
r.value.type === "text" &&
|
||||
r.value.textContent
|
||||
) {
|
||||
map.set(urlArr[i], r.value.textContent);
|
||||
if (r.status !== "fulfilled") continue;
|
||||
const v = r.value;
|
||||
if (v.type === "text" && v.textContent) {
|
||||
textMap.set(urlArr[i], v.textContent);
|
||||
if (v.title) titleMap.set(urlArr[i], v.title);
|
||||
} else if (v.type === "image" && v.data && v.mimeType) {
|
||||
// Direct image link (or og:image followed from an HTML page) —
|
||||
// kept for vision analysis below.
|
||||
imageMap.set(urlArr[i], { data: v.data, mimeType: v.mimeType });
|
||||
}
|
||||
}
|
||||
return map;
|
||||
return { text: textMap, image: imageMap, title: titleMap };
|
||||
})();
|
||||
|
||||
const searxngPromise = (async () => {
|
||||
@@ -122,10 +145,11 @@ export async function runTextOnlyBatch(
|
||||
return map;
|
||||
})();
|
||||
|
||||
const [urlFetchMap, searxngResults] = await Promise.all([
|
||||
const [urlFetchMaps, searxngResults] = await Promise.all([
|
||||
urlFetchPromise,
|
||||
searxngPromise,
|
||||
]);
|
||||
const urlFetchMap = urlFetchMaps.text;
|
||||
|
||||
// Deduplicate identical short messages
|
||||
const shortContentGroups = new Map<string, MessageRecord[]>();
|
||||
@@ -171,25 +195,109 @@ export async function runTextOnlyBatch(
|
||||
const batch = subBatches[i];
|
||||
const targetIds = batch.map((t) => t.id);
|
||||
|
||||
// User reputation + profiles
|
||||
// User reputation + profiles (raw summary text — deduplicated into a
|
||||
// single <user_profiles> map per batch; messages only reference it).
|
||||
const userContexts = new Map<string, string>();
|
||||
const userProfiles = new Map<string, string>();
|
||||
const userProfiles = new Map<
|
||||
string,
|
||||
{
|
||||
text: string;
|
||||
asOf?: number | null;
|
||||
}
|
||||
>();
|
||||
for (const msg of batch) {
|
||||
if (!userContexts.has(msg.user_id)) {
|
||||
const rep = await initializeUserReputation(msg.user_id, msg.guild_id);
|
||||
userContexts.set(
|
||||
msg.user_id,
|
||||
`<user_reputation trust_score="${rep.trust_score}" />`,
|
||||
);
|
||||
const repAttrs = formatReputationAttrs(rep);
|
||||
let repXml = `<user_reputation ${repAttrs}/>`;
|
||||
// Repeat offenders get their last flagged messages as <user_history>
|
||||
// so the LLM can recognize PATTERNS (same scam link, repeated
|
||||
// provocation) — history is reference, never proof. Best-effort.
|
||||
if (rep.total_infractions > 0) {
|
||||
try {
|
||||
const history = await getUserRecentInfractions(msg.user_id, 2);
|
||||
const historyXml = buildUserHistoryXml(
|
||||
history.map((h) => ({
|
||||
content: h.content ?? "",
|
||||
severity: h.severity,
|
||||
created_at: h.created_at,
|
||||
})),
|
||||
);
|
||||
if (historyXml) {
|
||||
repXml = `<user_reputation ${repAttrs}>\n${historyXml}\n</user_reputation>`;
|
||||
}
|
||||
} catch {
|
||||
// history is a bonus — fall back to attrs-only reputation
|
||||
}
|
||||
}
|
||||
userContexts.set(msg.user_id, repXml);
|
||||
}
|
||||
if (!userProfiles.has(msg.user_id)) {
|
||||
const profile = await getUserProfile(msg.user_id);
|
||||
userProfiles.set(
|
||||
msg.user_id,
|
||||
profile
|
||||
? `<user_profile>${sanitizeAiContent(profile.profile_summary)}</user_profile>`
|
||||
: "",
|
||||
);
|
||||
userProfiles.set(msg.user_id, {
|
||||
text: profile?.profile_summary ?? "",
|
||||
asOf: profile?.last_analyzed_at ?? null,
|
||||
});
|
||||
}
|
||||
}
|
||||
const userProfilesBlock = buildUserProfilesBlock(userProfiles);
|
||||
|
||||
// ── URL images → multimodal vision evidence ─────────────────────────
|
||||
// The text batch fetches inline URLs; whenever one resolved to an image
|
||||
// (direct image link, or og:image followed from an HTML page), run the
|
||||
// vision model and append its description as media evidence. If any
|
||||
// message in the sub-batch produced image evidence, the prompt switches
|
||||
// to "mixed" mode so media-analysis instructions/examples are injected
|
||||
// — a link to media is analyzed as media, not as bare text.
|
||||
const batchImageEvidence = new Map<string, string[]>();
|
||||
let batchHasImageEvidence = false;
|
||||
const urlImages = urlFetchMaps.image;
|
||||
const urlTitles = urlFetchMaps.title;
|
||||
if (urlImages.size > 0) {
|
||||
const maxDim = config.AI_LLM_IMAGE_MAX_DIMENSION ?? 1024;
|
||||
const evidenceSets = await Promise.all(
|
||||
batch.map(async (msg) => {
|
||||
const content = getAnalysisContent(msg);
|
||||
const pics = extractUrlsFromText(content)
|
||||
.slice(0, 3)
|
||||
.filter((url) => urlImages.has(url));
|
||||
if (pics.length === 0) return { id: msg.id, lines: [] as string[] };
|
||||
const lines = await Promise.all(
|
||||
pics.map(async (url) => {
|
||||
const img = urlImages.get(url)!;
|
||||
try {
|
||||
const { data: resizedBuffer, mimeType: resizedMime } =
|
||||
await resizeImageForVision(img.data, maxDim);
|
||||
const part: MessageImagePart = {
|
||||
type: "image_url",
|
||||
image_url: {
|
||||
url: `data:${resizedMime};base64,${resizedBuffer.toString("base64")}`,
|
||||
},
|
||||
sourceLabel: `[gambar dari URL ${url} (inline), pesan id=${msg.id}]`,
|
||||
};
|
||||
// Bound vision time so a dead vision model can't stall the
|
||||
// whole text batch — a timeout just skips the evidence.
|
||||
const timedOut = delay(15000).then(() => null as string | null);
|
||||
return await Promise.race([
|
||||
analyzeSingleMediaImage(msg.id, part),
|
||||
timedOut,
|
||||
]);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}),
|
||||
);
|
||||
return {
|
||||
id: msg.id,
|
||||
lines: lines.filter((l): l is string => Boolean(l)),
|
||||
};
|
||||
}),
|
||||
);
|
||||
for (const set of evidenceSets) {
|
||||
if (set.lines.length > 0) {
|
||||
batchImageEvidence.set(set.id, set.lines);
|
||||
batchHasImageEvidence = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,8 +312,7 @@ export async function runTextOnlyBatch(
|
||||
: undefined;
|
||||
const correctedExamples = await buildCorrectedFewShotExamples();
|
||||
const systemText = buildSystemPromptModular({
|
||||
contextText,
|
||||
mode: "text",
|
||||
mode: batchHasImageEvidence ? "mixed" : "text",
|
||||
correction,
|
||||
correctedExamples,
|
||||
channelCulture,
|
||||
@@ -214,38 +321,58 @@ export async function runTextOnlyBatch(
|
||||
const messagesBlock = (
|
||||
await Promise.all(
|
||||
batch.map(async (msg) => {
|
||||
const content = getAnalysisContent(msg);
|
||||
const content = truncateForAi(getAnalysisContent(msg));
|
||||
const msgUrls = extractUrlsFromText(content);
|
||||
const urlContexts = msgUrls
|
||||
.map((url) => {
|
||||
const ft = urlFetchMap.get(url);
|
||||
return ft
|
||||
? `<web_content url="${escapeXml(url)}">${escapeXml(ft)}</web_content>`
|
||||
: null;
|
||||
if (!ft) return null;
|
||||
const title = urlTitles.get(url);
|
||||
const titleAttr = title ? ` title="${escapeXml(title)}"` : "";
|
||||
return `<web_content url="${escapeXml(url)}"${titleAttr}>${escapeXml(ft)}</web_content>`;
|
||||
})
|
||||
.filter(Boolean)
|
||||
.join("\n");
|
||||
const webContext = urlContexts ? `\n${urlContexts}` : "";
|
||||
const mediaEvidenceCtx = (batchImageEvidence.get(msg.id) ?? [])
|
||||
.map((line) => `\n${line}`)
|
||||
.join("");
|
||||
const userCtx = userContexts.get(msg.user_id) ?? "";
|
||||
const userProfileCtx = userProfiles.get(msg.user_id) ?? "";
|
||||
const userProfileRef = (
|
||||
userProfiles.get(msg.user_id)?.text ?? ""
|
||||
).trim()
|
||||
? buildUserProfileRef(msg.user_id)
|
||||
: "";
|
||||
const refXml = await buildReferenceXml(msg);
|
||||
return `<message id="${msg.id}" user="${msg.username}">\n ${userCtx}${userProfileCtx ? `\n ${userProfileCtx}` : ""}${refXml ? `\n ${refXml}` : ""}\n <content>${escapeXml(content)}</content>${webContext}\n</message>`;
|
||||
const repetitionCount = groupMapping.get(msg.id)?.length ?? 1;
|
||||
const isBot = resolveIsBot(msg);
|
||||
const isEdited = resolveIsEdited(msg);
|
||||
return `<message id="${escapeXml(msg.id)}" user="${escapeXml(resolveDisplayName(msg))}" time="${new Date(msg.created_at).toISOString()}"${repetitionCount > 1 ? ` repetitions="${repetitionCount}"` : ""}${isBot ? ` bot="true"` : ""}${isEdited ? ` edited="true"` : ""}>\n ${userCtx}${userProfileRef ? `\n ${userProfileRef}` : ""}${refXml ? `\n ${refXml}` : ""}\n <content>${escapeXml(content)}</content>${webContext}${mediaEvidenceCtx}\n</message>`;
|
||||
}),
|
||||
)
|
||||
).join("\n");
|
||||
|
||||
const searxngBlock =
|
||||
searxngResults.size > 0
|
||||
? `\n\n<web_searches>\n${Array.from(searxngResults.entries())
|
||||
? `<web_searches>\n${Array.from(searxngResults.entries())
|
||||
.map(
|
||||
([q, xml]) =>
|
||||
` <search_query query="${escapeXml(q)}">\n${xml} </search_query>`,
|
||||
)
|
||||
.join("\n")}\n</web_searches>`
|
||||
: "";
|
||||
// Data/instruction separation: the system prompt is stable per mode —
|
||||
// all per-batch context (profiles, conversation, web evidence) lives in
|
||||
// the USER payload, ordered oldest-first so targets come last.
|
||||
const userBlocks = [
|
||||
userProfilesBlock?.trimEnd() ?? "",
|
||||
contextBlock?.trimEnd() ?? "",
|
||||
searxngBlock,
|
||||
`<messages_to_analyze>\n${messagesBlock}\n</messages_to_analyze>`,
|
||||
].filter((b) => b.trim().length > 0);
|
||||
return {
|
||||
system: systemText,
|
||||
user: `${searxngBlock}\n\n<messages_to_analyze>\n${messagesBlock}\n</messages_to_analyze>`,
|
||||
user: userBlocks.join("\n\n"),
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ export interface FetchedUrlContext {
|
||||
data?: Buffer;
|
||||
mimeType?: string;
|
||||
textContent?: string;
|
||||
/** Page title from og:title / <title> — strong signal for the LLM. */
|
||||
title?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
@@ -86,6 +88,50 @@ function extractOgImage(html: string): string | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
export interface OgMeta {
|
||||
title: string | null;
|
||||
description: string | null;
|
||||
siteName: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts OpenGraph / twitter meta + <title> from raw HTML. Both attribute
|
||||
* orders are accepted (<meta property=... content=...> and reversed).
|
||||
*/
|
||||
export function extractOgMeta(html: string): OgMeta {
|
||||
const metaValue = (name: string): string | null => {
|
||||
const re = new RegExp(
|
||||
`<meta[^>]*(?:property|name)=["']${name}["'][^>]*content=["']([^"']+)["']`,
|
||||
"i",
|
||||
);
|
||||
const m = html.match(re);
|
||||
if (m?.[1]) return m[1].replace(/&/g, "&").replace(/"/g, '"');
|
||||
const reRev = new RegExp(
|
||||
`<meta[^>]*content=["']([^"']+)["'][^>]*(?:property|name)=["']${name}["']`,
|
||||
"i",
|
||||
);
|
||||
const mRev = html.match(reRev);
|
||||
return mRev?.[1]
|
||||
? mRev[1].replace(/&/g, "&").replace(/"/g, '"')
|
||||
: null;
|
||||
};
|
||||
|
||||
const title =
|
||||
metaValue("og:title") ||
|
||||
metaValue("twitter:title") ||
|
||||
html.match(/<title[^>]*>([^<]+)<\/title>/i)?.[1]?.trim() ||
|
||||
null;
|
||||
const description =
|
||||
metaValue("og:description") ||
|
||||
metaValue("twitter:description") ||
|
||||
metaValue("description") ||
|
||||
null;
|
||||
const siteName =
|
||||
metaValue("og:site_name") || metaValue("application-name") || null;
|
||||
|
||||
return { title, description, siteName };
|
||||
}
|
||||
|
||||
function truncateAndCleanHtml(html: string, maxLen = 1000): string {
|
||||
// Strip <script> and <style> entirely
|
||||
let text = html.replace(
|
||||
@@ -176,6 +222,7 @@ export async function fetchUrlSafely(
|
||||
url,
|
||||
type: "text",
|
||||
textContent: cleaned,
|
||||
title: extractOgMeta(text).title ?? undefined,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -37,15 +37,21 @@ import {
|
||||
} from "./mediaDownloader.js";
|
||||
import {
|
||||
buildReferenceXml,
|
||||
buildUserHistoryXml,
|
||||
buildUserProfileRef,
|
||||
escapeXml,
|
||||
formatReputationAttrs,
|
||||
getAnalysisContent,
|
||||
resolveDisplayName,
|
||||
resolveIsBot,
|
||||
resolveIsEdited,
|
||||
truncateForAi,
|
||||
} from "./moderationBuilders.js";
|
||||
import {
|
||||
buildCustomEmojiVisionPrompt,
|
||||
buildGeneralImageVisionPrompt,
|
||||
buildStickerTextOnlyWarning,
|
||||
buildStickerVisionPrompt,
|
||||
sanitizeAiContent,
|
||||
} from "./moderationPrompt.js";
|
||||
import {
|
||||
extractSearchQueries,
|
||||
@@ -54,7 +60,10 @@ import {
|
||||
} from "./searxngSearch.js";
|
||||
import { extractUrlsFromText } from "./urlFetcher.js";
|
||||
import { getUserProfile } from "./userProfileStore.js";
|
||||
import { initializeUserReputation } from "./userReputationStore.js";
|
||||
import {
|
||||
getUserRecentInfractions,
|
||||
initializeUserReputation,
|
||||
} from "./userReputationStore.js";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
@@ -366,7 +375,38 @@ export async function prepareMediaMessage(
|
||||
const rep = await initializeUserReputation(target.user_id, target.guild_id);
|
||||
const profile = await getUserProfile(target.user_id);
|
||||
const refXml = await buildReferenceXml(target);
|
||||
// Profile is emitted ONCE per batch in a <user_profiles> map (see
|
||||
// mediaBatchProcessor); here we only reference it to avoid repeating the
|
||||
// full summary on every message of the same user.
|
||||
const profileRef =
|
||||
profile && profile.profile_summary?.trim()
|
||||
? buildUserProfileRef(target.user_id)
|
||||
: "";
|
||||
|
||||
const messageBlock = `<message id="${escapeXml(target.id)}" user="${escapeXml(target.username)}">\n <user_reputation trust_score="${rep.trust_score}" />${profile ? `\n <user_profile>${sanitizeAiContent(profile.profile_summary)}</user_profile>` : ""}${refXml ? `\n ${refXml}` : ""}\n <content>${escapeXml(content)}</content>${mediaContext ? ` ${escapeXml(mediaContext)}` : ""}${webContext}${mediaAnalysisContext}${searxngXml}\n</message>`;
|
||||
// Rich reputation — same shape as the text path: attrs + optional
|
||||
// <user_history> with the last flagged messages for repeat offenders.
|
||||
const repAttrs = formatReputationAttrs(rep);
|
||||
let repXml = `<user_reputation ${repAttrs}/>`;
|
||||
if (rep.total_infractions > 0) {
|
||||
try {
|
||||
const history = await getUserRecentInfractions(target.user_id, 2);
|
||||
const historyXml = buildUserHistoryXml(
|
||||
history.map((h) => ({
|
||||
content: h.content ?? "",
|
||||
severity: h.severity,
|
||||
created_at: h.created_at,
|
||||
})),
|
||||
);
|
||||
if (historyXml) {
|
||||
repXml = `<user_reputation ${repAttrs}>\n${historyXml}\n</user_reputation>`;
|
||||
}
|
||||
} catch {
|
||||
// history is a bonus — fall back to attrs-only reputation
|
||||
}
|
||||
}
|
||||
|
||||
const isBot = resolveIsBot(target);
|
||||
const isEdited = resolveIsEdited(target);
|
||||
const messageBlock = `<message id="${escapeXml(target.id)}" user="${escapeXml(resolveDisplayName(target))}" time="${new Date(target.created_at).toISOString()}"${isBot ? ` bot="true"` : ""}${isEdited ? ` edited="true"` : ""}>\n ${repXml}${profileRef ? `\n ${profileRef}` : ""}${refXml ? `\n ${refXml}` : ""}\n <content>${escapeXml(truncateForAi(content))}</content>${mediaContext ? ` ${escapeXml(mediaContext)}` : ""}${webContext}${mediaAnalysisContext}${searxngXml}\n</message>`;
|
||||
return { targetId, messageBlock };
|
||||
}
|
||||
|
||||
@@ -9,6 +9,10 @@ export interface MessageLocation {
|
||||
threadId: string | null;
|
||||
threadName: string | null;
|
||||
channelName: string | null;
|
||||
/** Channel topic (resmi/deskripsi channel) — strong context for judging
|
||||
* whether a message fits the channel's purpose. Guarded: some channel
|
||||
* types (threads on older API builds) expose no topic. */
|
||||
topic?: string | null;
|
||||
nsfw?: boolean;
|
||||
nsfwLevel?: string | null;
|
||||
ageRestricted?: boolean;
|
||||
@@ -107,12 +111,17 @@ export function getMessageLocation(message: Message): MessageLocation {
|
||||
nsfw?: boolean;
|
||||
nsfwLevel?: string | null;
|
||||
};
|
||||
const topic =
|
||||
"topic" in channel && typeof channel.topic === "string"
|
||||
? channel.topic
|
||||
: null;
|
||||
if (!channel.isThread?.()) {
|
||||
return {
|
||||
channelId: message.channelId,
|
||||
threadId: null,
|
||||
threadName: null,
|
||||
channelName: "name" in channel ? channel.name : null,
|
||||
topic,
|
||||
nsfw:
|
||||
typeof safetyChannel.nsfw === "boolean"
|
||||
? safetyChannel.nsfw
|
||||
@@ -133,6 +142,7 @@ export function getMessageLocation(message: Message): MessageLocation {
|
||||
threadId: channel.id,
|
||||
threadName: channel.name,
|
||||
channelName: channel.parent?.name ?? null,
|
||||
topic,
|
||||
nsfw:
|
||||
typeof safetyChannel.nsfw === "boolean" ? safetyChannel.nsfw : undefined,
|
||||
nsfwLevel:
|
||||
|
||||
@@ -189,6 +189,17 @@ export const configSchema = z
|
||||
.int()
|
||||
.positive()
|
||||
.default(20),
|
||||
// Recency gates for conversation context. A silence longer than GAP_MS
|
||||
// between context messages = the conversation restarted (older messages
|
||||
// dropped); MAX_AGE_MS caps how far back context is considered relevant.
|
||||
AI_ANALYSIS_CONTEXT_GAP_MS: z.coerce
|
||||
.number()
|
||||
.positive()
|
||||
.default(12 * 60 * 1000),
|
||||
AI_ANALYSIS_CONTEXT_MAX_AGE_MS: z.coerce
|
||||
.number()
|
||||
.positive()
|
||||
.default(45 * 60 * 1000),
|
||||
AI_ANALYSIS_PROCESSING_TIMEOUT_MS: z.coerce
|
||||
.number()
|
||||
.positive()
|
||||
@@ -242,6 +253,19 @@ export const configSchema = z
|
||||
.default(false),
|
||||
AUTO_DELETE_LOG_CHANNEL_ID: z.string().default(""),
|
||||
|
||||
// ── Nickname Reset (offensive_username enforcement) ────────────────
|
||||
// When the only violation is the member's server nickname, reset the
|
||||
// nickname to the default username instead of deleting the message.
|
||||
AUTO_NICKNAME_RESET_ENABLED: z
|
||||
.string()
|
||||
.optional()
|
||||
.transform((v) => v === "true")
|
||||
.default(true),
|
||||
AUTO_NICKNAME_RESET_COOLDOWN_MS: z.coerce
|
||||
.number()
|
||||
.positive()
|
||||
.default(10 * 60 * 1000),
|
||||
|
||||
// ── Retention ───────────────────────────────────────────────────────
|
||||
RETENTION_MESSAGES_DAYS: z.coerce.number().int().min(0).default(0),
|
||||
RETENTION_ATTACHMENTS_DAYS: z.coerce.number().int().min(0).default(0),
|
||||
|
||||
@@ -615,6 +615,7 @@ export const pgModerationActionsTable = pgTable(
|
||||
"warn_user",
|
||||
"kick_user",
|
||||
"ban_user",
|
||||
"reset_nickname",
|
||||
],
|
||||
}).notNull(),
|
||||
reason: pgText("reason"),
|
||||
|
||||
@@ -198,7 +198,8 @@ export type ModerationActionType =
|
||||
| "mute_user"
|
||||
| "warn_user"
|
||||
| "kick_user"
|
||||
| "ban_user";
|
||||
| "ban_user"
|
||||
| "reset_nickname";
|
||||
|
||||
export interface ModerationAction {
|
||||
id: string;
|
||||
|
||||
@@ -0,0 +1,209 @@
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Context enrichment builders — rich <user_reputation> attrs, <user_history>,
|
||||
// <user_profiles> as_of, bot/edited detection (pure, no DB)
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildUserHistoryXml,
|
||||
buildUserProfilesBlock,
|
||||
formatReputationAttrs,
|
||||
resolveIsBot,
|
||||
resolveIsEdited,
|
||||
} from "../src/modules/ai-moderation/moderationBuilders.js";
|
||||
import type { MessageRecord } from "../src/modules/message-capture/types.js";
|
||||
|
||||
const NOW = 1_800_000_000_000;
|
||||
|
||||
function msg(overrides: Partial<MessageRecord> = {}): MessageRecord {
|
||||
return {
|
||||
id: "m1",
|
||||
guild_id: "g1",
|
||||
channel_id: "c1",
|
||||
thread_id: null,
|
||||
user_id: "u1",
|
||||
username: "user1",
|
||||
avatar_url: null,
|
||||
content: "hai",
|
||||
edited_content: null,
|
||||
created_at: NOW,
|
||||
edited_at: null,
|
||||
deleted_at: null,
|
||||
type: "text",
|
||||
is_reply: null,
|
||||
is_forward: null,
|
||||
is_crosspost: null,
|
||||
reference_message_id: null,
|
||||
reference_channel_id: null,
|
||||
reference_guild_id: null,
|
||||
metadata: null,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
describe("formatReputationAttrs — rich reputation signal", () => {
|
||||
it("emits trust, infraction count and clean streak", () => {
|
||||
const attrs = formatReputationAttrs({
|
||||
trust_score: 62,
|
||||
total_infractions: 3,
|
||||
clean_message_streak: 45,
|
||||
last_infraction_at: null,
|
||||
});
|
||||
expect(attrs).toContain('trust_score="62"');
|
||||
expect(attrs).toContain('total_infractions="3"');
|
||||
expect(attrs).toContain('clean_streak="45"');
|
||||
});
|
||||
|
||||
it("derives last_offense_days_ago and marks repeat offenders (7-day window)", () => {
|
||||
const attrs = formatReputationAttrs(
|
||||
{
|
||||
trust_score: 50,
|
||||
total_infractions: 2,
|
||||
clean_message_streak: 0,
|
||||
last_infraction_at: NOW - 2 * DAY_MS,
|
||||
},
|
||||
NOW,
|
||||
);
|
||||
expect(attrs).toContain('last_offense_days_ago="2"');
|
||||
expect(attrs).toContain('repeat_offender="true"');
|
||||
});
|
||||
|
||||
it("does NOT mark repeat offender when the last offense is older than 7 days", () => {
|
||||
const attrs = formatReputationAttrs(
|
||||
{
|
||||
trust_score: 50,
|
||||
total_infractions: 2,
|
||||
clean_message_streak: 10,
|
||||
last_infraction_at: NOW - 30 * DAY_MS,
|
||||
},
|
||||
NOW,
|
||||
);
|
||||
expect(attrs).toContain('last_offense_days_ago="30"');
|
||||
expect(attrs).not.toContain("repeat_offender");
|
||||
});
|
||||
|
||||
it("omits offense-derived attrs when the user has no recorded infraction date", () => {
|
||||
const attrs = formatReputationAttrs({
|
||||
trust_score: 85,
|
||||
total_infractions: 0,
|
||||
clean_message_streak: 120,
|
||||
last_infraction_at: null,
|
||||
});
|
||||
expect(attrs).not.toContain("last_offense_days_ago");
|
||||
expect(attrs).not.toContain("repeat_offender");
|
||||
});
|
||||
|
||||
it("clamps a future/skewed timestamp to days_ago=0", () => {
|
||||
const attrs = formatReputationAttrs(
|
||||
{
|
||||
trust_score: 50,
|
||||
total_infractions: 1,
|
||||
clean_message_streak: 0,
|
||||
last_infraction_at: NOW + 5 * DAY_MS,
|
||||
},
|
||||
NOW,
|
||||
);
|
||||
expect(attrs).toContain('last_offense_days_ago="0"');
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildUserHistoryXml — last flagged messages for repeat offenders", () => {
|
||||
it("returns empty when there is no real history", () => {
|
||||
expect(buildUserHistoryXml([])).toBe("");
|
||||
expect(
|
||||
buildUserHistoryXml([{ content: " ", severity: "low", created_at: 1 }]),
|
||||
).toBe("");
|
||||
});
|
||||
|
||||
it("renders <infraction> rows with severity and recency", () => {
|
||||
const xml = buildUserHistoryXml(
|
||||
[
|
||||
{
|
||||
content: "beli barang murah disini https://scam.example",
|
||||
severity: "high",
|
||||
created_at: NOW - 3 * DAY_MS,
|
||||
},
|
||||
],
|
||||
NOW,
|
||||
);
|
||||
expect(xml).toContain("<user_history>");
|
||||
expect(xml).toContain('severity="high"');
|
||||
expect(xml).toContain('time_ago_days="3"');
|
||||
expect(xml).toContain("beli barang murah disini");
|
||||
});
|
||||
|
||||
it("caps long snippets and XML-escapes content", () => {
|
||||
const xml = buildUserHistoryXml(
|
||||
[
|
||||
{
|
||||
content: "x".repeat(300),
|
||||
severity: "low",
|
||||
created_at: NOW - DAY_MS,
|
||||
},
|
||||
],
|
||||
NOW,
|
||||
);
|
||||
expect(xml.length).toBeLessThan(250);
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildUserProfilesBlock — deduplicated map with staleness", () => {
|
||||
it("emits as_of when the profile has a last-generated timestamp", () => {
|
||||
const block = buildUserProfilesBlock(
|
||||
new Map([
|
||||
[
|
||||
"u1",
|
||||
{
|
||||
text: "Developer teknis, bahasa Indonesia",
|
||||
asOf: NOW - 3 * DAY_MS,
|
||||
},
|
||||
],
|
||||
]),
|
||||
);
|
||||
expect(block).toContain('<user_profile user_id="u1"');
|
||||
expect(block).toContain(
|
||||
`as_of="${new Date(NOW - 3 * DAY_MS).toISOString()}"`,
|
||||
);
|
||||
expect(block).toContain("Developer teknis");
|
||||
});
|
||||
|
||||
it("omits as_of when absent, and drops empty profiles", () => {
|
||||
const block = buildUserProfilesBlock(
|
||||
new Map([
|
||||
["u1", { text: "profil aktif", asOf: null }],
|
||||
["u2", { text: " " }],
|
||||
]),
|
||||
);
|
||||
expect(block).toContain('user_id="u1"');
|
||||
expect(block).not.toContain("as_of");
|
||||
expect(block).not.toContain("u2");
|
||||
});
|
||||
|
||||
it("returns empty for no profiles", () => {
|
||||
expect(buildUserProfilesBlock(new Map())).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveIsBot / resolveIsEdited — message flags", () => {
|
||||
it("reads author.bot from captured metadata", () => {
|
||||
const bot = msg({
|
||||
metadata: JSON.stringify({
|
||||
author: { id: "x", username: "bot", bot: true },
|
||||
}),
|
||||
});
|
||||
const human = msg({
|
||||
metadata: JSON.stringify({
|
||||
author: { id: "y", username: "user", bot: false },
|
||||
}),
|
||||
});
|
||||
expect(resolveIsBot(bot)).toBe(true);
|
||||
expect(resolveIsBot(human)).toBe(false);
|
||||
expect(resolveIsBot(msg())).toBe(false);
|
||||
});
|
||||
|
||||
it("flags edited content only when edited_content is present (the edit path)", () => {
|
||||
expect(resolveIsEdited(msg({ edited_content: "versi baru" }))).toBe(true);
|
||||
expect(resolveIsEdited(msg())).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,299 @@
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Conversation context v2 — recency gating + location context (pure, no DB)
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
buildConversationContext,
|
||||
buildLocationContext,
|
||||
formatMessageForPrompt,
|
||||
truncateContextLine,
|
||||
} from "../src/modules/ai-moderation/conversationContext.js";
|
||||
import { buildConversationContextBlock } from "../src/modules/ai-moderation/moderationBuilders.js";
|
||||
import { extractOgMeta } from "../src/modules/ai-moderation/urlFetcher.js";
|
||||
import type { MessageRecord } from "../src/modules/message-capture/types.js";
|
||||
|
||||
const NOW = 1_800_000_000_000;
|
||||
|
||||
function msg(id: string, createdAt: number, content = "hai"): MessageRecord {
|
||||
return {
|
||||
id,
|
||||
guild_id: "g1",
|
||||
channel_id: "c1",
|
||||
thread_id: null,
|
||||
user_id: `u_${id}`,
|
||||
username: `user_${id}`,
|
||||
avatar_url: null,
|
||||
content,
|
||||
edited_content: null,
|
||||
created_at: createdAt,
|
||||
edited_at: null,
|
||||
deleted_at: null,
|
||||
type: "text",
|
||||
is_reply: null,
|
||||
is_forward: null,
|
||||
is_crosspost: null,
|
||||
reference_message_id: null,
|
||||
reference_channel_id: null,
|
||||
reference_guild_id: null,
|
||||
metadata: null,
|
||||
};
|
||||
}
|
||||
|
||||
function target(id = "t1", createdAt = NOW): MessageRecord {
|
||||
return {
|
||||
...msg(id, createdAt),
|
||||
content: "pesan yang dianalisis",
|
||||
};
|
||||
}
|
||||
|
||||
const MIN = 60_000;
|
||||
|
||||
describe("buildConversationContext — recency gating", () => {
|
||||
it("keeps an ONGOING conversation — recent messages, small gaps", () => {
|
||||
const context = [
|
||||
msg("a", NOW - 8 * MIN),
|
||||
msg("b", NOW - 6 * MIN),
|
||||
msg("c", NOW - 4 * MIN),
|
||||
msg("d", NOW - 2 * MIN),
|
||||
];
|
||||
const { lines, descriptor, dropped } = buildConversationContext({
|
||||
contextBefore: context,
|
||||
targets: [target()],
|
||||
maxTokens: 8000,
|
||||
gapMs: 12 * MIN,
|
||||
maxAgeMs: 45 * MIN,
|
||||
});
|
||||
expect(lines).toHaveLength(4);
|
||||
expect(dropped).toBe(0);
|
||||
expect(descriptor).toContain("status=ongoing");
|
||||
});
|
||||
|
||||
it("drops messages before a silence gap — conversation RESTARTED", () => {
|
||||
const context = [
|
||||
msg("old1", NOW - 40 * MIN),
|
||||
msg("old2", NOW - 38 * MIN),
|
||||
msg("fresh", NOW - 5 * MIN),
|
||||
];
|
||||
const { lines, descriptor, dropped } = buildConversationContext({
|
||||
contextBefore: context,
|
||||
targets: [target()],
|
||||
maxTokens: 8000,
|
||||
gapMs: 12 * MIN,
|
||||
maxAgeMs: 45 * MIN,
|
||||
});
|
||||
// 40min-old messages are within maxAge but 33min before "fresh" → gap gate
|
||||
expect(lines.some((l) => l.includes("old1"))).toBe(false);
|
||||
expect(lines.some((l) => l.includes("fresh"))).toBe(true);
|
||||
expect(dropped).toBe(2);
|
||||
expect(descriptor).toContain("status=sparse");
|
||||
expect(descriptor).toContain("gap_before_min=");
|
||||
});
|
||||
|
||||
it("drops everything older than maxAge — stale noise, cold_start anchor kept", () => {
|
||||
const context = [
|
||||
msg("ancient", NOW - 120 * MIN),
|
||||
msg("stale", NOW - 60 * MIN),
|
||||
];
|
||||
const { lines, descriptor, dropped } = buildConversationContext({
|
||||
contextBefore: context,
|
||||
targets: [target()],
|
||||
maxTokens: 8000,
|
||||
gapMs: 12 * MIN,
|
||||
maxAgeMs: 45 * MIN,
|
||||
});
|
||||
// Age gate drops both from the real context block, but the cold-start
|
||||
// anchor keeps the nearest 2 so the LLM still senses the channel.
|
||||
expect(dropped).toBe(2);
|
||||
expect(descriptor).toContain("status=cold_start");
|
||||
expect(lines).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("keeps a 2-message anchor on cold start so the LLM senses the channel", () => {
|
||||
const context = [
|
||||
msg("far1", NOW - 100 * MIN),
|
||||
msg("far2", NOW - 99 * MIN),
|
||||
msg("near1", NOW - 50 * MIN),
|
||||
];
|
||||
const { lines, descriptor } = buildConversationContext({
|
||||
contextBefore: context,
|
||||
targets: [target()],
|
||||
maxTokens: 8000,
|
||||
gapMs: 12 * MIN,
|
||||
maxAgeMs: 45 * MIN,
|
||||
});
|
||||
expect(lines).toHaveLength(2); // nearest 2 kept as anchor
|
||||
expect(lines.some((l) => l.includes("near1"))).toBe(true);
|
||||
expect(descriptor).toContain("status=cold_start");
|
||||
});
|
||||
|
||||
it("respects the token budget (older lines dropped first)", () => {
|
||||
const context = Array.from({ length: 20 }, (_, i) =>
|
||||
msg(`m${i}`, NOW - (i + 1) * MIN),
|
||||
);
|
||||
const { lines } = buildConversationContext({
|
||||
contextBefore: context,
|
||||
targets: [target()],
|
||||
maxTokens: 600,
|
||||
gapMs: 12 * MIN,
|
||||
maxAgeMs: 45 * MIN,
|
||||
});
|
||||
expect(lines.length).toBeLessThan(20);
|
||||
expect(lines.length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatMessageForPrompt — server nickname (displayName)", () => {
|
||||
it("renders member.displayName when captured (per-server nickname)", () => {
|
||||
const m = msg("n1", NOW - MIN);
|
||||
m.metadata = JSON.stringify({
|
||||
member: {
|
||||
displayName: "Si Goblok Server",
|
||||
roles: [],
|
||||
joinedTimestamp: null,
|
||||
},
|
||||
});
|
||||
const line = formatMessageForPrompt(m, "context");
|
||||
expect(line).toContain("user=Si Goblok Server");
|
||||
expect(line).not.toContain("user_user_n1");
|
||||
});
|
||||
|
||||
it("falls back to global username when displayName missing", () => {
|
||||
const line = formatMessageForPrompt(
|
||||
msg("n2", NOW - MIN, "halo"),
|
||||
"context",
|
||||
);
|
||||
expect(line).toContain("user=user_n2");
|
||||
});
|
||||
|
||||
it("truncates an oversized context message so one paste cannot eat the whole budget", () => {
|
||||
const huge = "A".repeat(5000);
|
||||
const line = formatMessageForPrompt(msg("n3", NOW - MIN, huge), "context");
|
||||
expect(line).toContain("…[konteks dipotong: terlalu panjang]");
|
||||
expect(line.length).toBeLessThan(2000);
|
||||
});
|
||||
|
||||
it("keeps short context content intact", () => {
|
||||
expect(truncateContextLine("pendek")).toBe("pendek");
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildLocationContext — channel/thread/nsfw enrichment", () => {
|
||||
it("renders a structured <location_context/> element from captured metadata", () => {
|
||||
const t = target();
|
||||
t.metadata = JSON.stringify({
|
||||
channel: {
|
||||
channelName: "general",
|
||||
threadName: "tanya coding",
|
||||
nsfw: false,
|
||||
ageRestricted: false,
|
||||
},
|
||||
});
|
||||
const line = buildLocationContext([t]);
|
||||
expect(line).toContain("<location_context");
|
||||
expect(line).toContain('channel_id="c1"');
|
||||
expect(line).toContain('channel_name="general"');
|
||||
expect(line).toContain('thread_name="tanya coding"');
|
||||
expect(line).toContain('nsfw="false"');
|
||||
expect(line).toContain('age_restricted="false"');
|
||||
});
|
||||
|
||||
it("includes the channel topic (escaped) when captured", () => {
|
||||
const t = target();
|
||||
t.metadata = JSON.stringify({
|
||||
channel: {
|
||||
channelName: "rules",
|
||||
topic: "Diskusi coding & programming — no self-promo",
|
||||
nsfw: false,
|
||||
},
|
||||
});
|
||||
const line = buildLocationContext([t]);
|
||||
expect(line).toContain(
|
||||
'topic="Diskusi coding & programming — no self-promo"',
|
||||
);
|
||||
});
|
||||
|
||||
it("caps an oversized topic and omits empty/absent topic", () => {
|
||||
const t = target();
|
||||
t.metadata = JSON.stringify({
|
||||
channel: { channelName: "general", topic: "x".repeat(500), nsfw: false },
|
||||
});
|
||||
const line = buildLocationContext([t]);
|
||||
const match = line.match(/topic="([^"]*)"/);
|
||||
expect(match).not.toBeNull();
|
||||
expect(match?.[1].length).toBeLessThanOrEqual(201);
|
||||
|
||||
const t2 = target();
|
||||
t2.metadata = JSON.stringify({ channel: { channelName: "general" } });
|
||||
expect(buildLocationContext([t2])).not.toContain("topic=");
|
||||
});
|
||||
|
||||
it("returns empty when no metadata", () => {
|
||||
expect(buildLocationContext([target()])).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildConversationContextBlock — structured USER-message context", () => {
|
||||
it("wraps location + descriptor + lines into XML blocks", () => {
|
||||
const block = buildConversationContextBlock({
|
||||
location: buildLocationContext(
|
||||
(() => {
|
||||
const t = target();
|
||||
t.metadata = JSON.stringify({
|
||||
channel: { channelName: "general", nsfw: false },
|
||||
});
|
||||
return [t];
|
||||
})(),
|
||||
),
|
||||
descriptor: "[conversation_flow] status=ongoing context_msgs=1 dropped=0",
|
||||
lines: ["[context] id=a time=2027-01-01T00:00:00.000Z user=user_a: hai"],
|
||||
});
|
||||
expect(block).toContain("<location_context");
|
||||
expect(block).toContain("<conversation_context>");
|
||||
expect(block).toContain("[conversation_flow] status=ongoing");
|
||||
expect(block).toContain("[context] id=a");
|
||||
// location block comes before conversation block
|
||||
expect(block.indexOf("<location_context")).toBeLessThan(
|
||||
block.indexOf("<conversation_context>"),
|
||||
);
|
||||
});
|
||||
|
||||
it("omits the conversation block when there are no lines", () => {
|
||||
const block = buildConversationContextBlock({
|
||||
location: "",
|
||||
descriptor: "",
|
||||
lines: [],
|
||||
});
|
||||
expect(block).toBe("");
|
||||
});
|
||||
|
||||
it("keeps only the location block when lines are empty but location exists", () => {
|
||||
const block = buildConversationContextBlock({
|
||||
location: '<location_context channel_id="c1"/>',
|
||||
descriptor: "",
|
||||
lines: [],
|
||||
});
|
||||
expect(block).toBe('<location_context channel_id="c1"/>');
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractOgMeta — page title/site for <web_content>", () => {
|
||||
it("extracts og:title, og:description and og:site_name", () => {
|
||||
const html = `
|
||||
<html><head>
|
||||
<title>Fallback title</title>
|
||||
<meta property="og:title" content="Judul Halaman & Keren" />
|
||||
<meta property="og:description" content="Deskripsi halaman" />
|
||||
<meta property="og:site_name" content="Contoh Site" />
|
||||
<meta property="og:image" content="https://img.example.com/x.png" />
|
||||
</head></html>`;
|
||||
const meta = extractOgMeta(html);
|
||||
expect(meta.title).toBe("Judul Halaman & Keren");
|
||||
expect(meta.description).toBe("Deskripsi halaman");
|
||||
expect(meta.siteName).toBe("Contoh Site");
|
||||
});
|
||||
|
||||
it("falls back to <title> when og:title missing", () => {
|
||||
const html = "<html><head><title>Plain Title</title></head></html>";
|
||||
expect(extractOgMeta(html).title).toBe("Plain Title");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,77 @@
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
// Nickname-only enforcement — offensive username flag handling (pure, no DB)
|
||||
// ═══════════════════════════════════════════════════════════════════════════
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
isNicknameOnlyViolation,
|
||||
parseModerationFlags,
|
||||
} from "../src/modules/ai-moderation/autoDeleteEligibility.js";
|
||||
import type {
|
||||
AnalysisResult,
|
||||
MessageRecord,
|
||||
} from "../src/modules/message-capture/types.js";
|
||||
|
||||
function msg(flagsJson: string | null): MessageRecord {
|
||||
return {
|
||||
id: "m1",
|
||||
guild_id: "g1",
|
||||
channel_id: "c1",
|
||||
thread_id: null,
|
||||
user_id: "u1",
|
||||
username: "user1",
|
||||
avatar_url: null,
|
||||
content: "halo semua",
|
||||
edited_content: null,
|
||||
created_at: Date.now(),
|
||||
edited_at: null,
|
||||
deleted_at: null,
|
||||
type: "text",
|
||||
is_reply: null,
|
||||
is_forward: null,
|
||||
is_crosspost: null,
|
||||
reference_message_id: null,
|
||||
reference_channel_id: null,
|
||||
reference_guild_id: null,
|
||||
metadata: null,
|
||||
ai_moderation_flags: flagsJson,
|
||||
};
|
||||
}
|
||||
|
||||
describe("parseModerationFlags", () => {
|
||||
it("parses JSON array from stored column", () => {
|
||||
expect(parseModerationFlags(msg('["offensive_username","sara"]'))).toEqual([
|
||||
"offensive_username",
|
||||
"sara",
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns [] for null / malformed values", () => {
|
||||
expect(parseModerationFlags(msg(null))).toEqual([]);
|
||||
expect(parseModerationFlags(msg("not-json"))).toEqual([]);
|
||||
});
|
||||
|
||||
it("prefers structured analysisResult flags", () => {
|
||||
const result = { flags: ["vulgar_language"] } as AnalysisResult;
|
||||
expect(parseModerationFlags(msg('["old_flag"]'), result)).toEqual([
|
||||
"vulgar_language",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isNicknameOnlyViolation", () => {
|
||||
it("true when the ONLY flag is offensive_username", () => {
|
||||
expect(isNicknameOnlyViolation(msg('["offensive_username"]'))).toBe(true);
|
||||
});
|
||||
|
||||
it("false when other flags ride along (message itself violated)", () => {
|
||||
expect(isNicknameOnlyViolation(msg('["offensive_username","sara"]'))).toBe(
|
||||
false,
|
||||
);
|
||||
expect(isNicknameOnlyViolation(msg('["harassment"]'))).toBe(false);
|
||||
});
|
||||
|
||||
it("false when no flags at all", () => {
|
||||
expect(isNicknameOnlyViolation(msg(null))).toBe(false);
|
||||
expect(isNicknameOnlyViolation(msg("[]"))).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,8 @@
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
// 1. AppError Hierarchy
|
||||
// ═══════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
AppError,
|
||||
ConfigError,
|
||||
@@ -9,7 +11,6 @@ import {
|
||||
UnauthorizedError,
|
||||
ValidationError,
|
||||
} from "../src/shared/errors/index.js";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
describe("AppError subclasses", () => {
|
||||
it("AppError carries code, statusCode, and details", () => {
|
||||
|
||||
@@ -56,6 +56,13 @@
|
||||
/* Ring / focus outline for shadcn outline-ring utility */
|
||||
--color-ring: var(--color-primary);
|
||||
|
||||
/* Base-ui / shadcn primitives */
|
||||
--color-popover: oklch(0.99 0.005 250 / 0.95);
|
||||
--color-popover-foreground: var(--color-text-primary);
|
||||
--color-input: oklch(0.55 0.02 250 / 0.35);
|
||||
--color-secondary: oklch(0.92 0.01 250 / 0.6);
|
||||
--color-secondary-foreground: var(--color-text-primary);
|
||||
|
||||
/* Sidebar (shadcn) */
|
||||
--color-sidebar: oklch(1 0 0 / 0.6);
|
||||
--color-sidebar-foreground: var(--color-text-primary);
|
||||
@@ -113,6 +120,13 @@
|
||||
--color-accent-foreground: var(--color-primary-foreground);
|
||||
--color-ring: var(--color-primary);
|
||||
|
||||
/* Base-ui / shadcn primitives */
|
||||
--color-popover: oklch(0.13 0.02 245 / 0.96);
|
||||
--color-popover-foreground: var(--color-text-primary);
|
||||
--color-input: oklch(1 0 0 / 0.18);
|
||||
--color-secondary: oklch(0.2 0.02 245 / 0.7);
|
||||
--color-secondary-foreground: var(--color-text-primary);
|
||||
|
||||
/* Sidebar (shadcn) */
|
||||
--color-sidebar: oklch(0.11 0.02 245 / 0.55);
|
||||
--color-sidebar-foreground: var(--color-text-primary);
|
||||
|
||||
@@ -63,7 +63,7 @@ function SelectContent({
|
||||
sideOffset = 4,
|
||||
align = "center",
|
||||
alignOffset = 0,
|
||||
alignItemWithTrigger = true,
|
||||
alignItemWithTrigger = false,
|
||||
...props
|
||||
}: SelectPrimitive.Popup.Props &
|
||||
Pick<
|
||||
@@ -83,7 +83,7 @@ function SelectContent({
|
||||
<SelectPrimitive.Popup
|
||||
data-slot="select-content"
|
||||
data-align-trigger={alignItemWithTrigger}
|
||||
className={cn("relative isolate z-50 max-h-(--available-height) w-(--anchor-width) min-w-36 origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-lg bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-[align-trigger=true]:animate-none data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className )}
|
||||
className={cn("relative isolate z-50 max-h-(--available-height) w-fit min-w-40 max-w-(--available-width) origin-(--transform-origin) overflow-x-hidden overflow-y-auto rounded-lg bg-popover text-popover-foreground shadow-md ring-1 ring-foreground/10 duration-100 data-[align-trigger=true]:animate-none data-[side=bottom]:slide-in-from-top-2 data-[side=inline-end]:slide-in-from-left-2 data-[side=inline-start]:slide-in-from-right-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-open:animate-in data-open:fade-in-0 data-open:zoom-in-95 data-closed:animate-out data-closed:fade-out-0 data-closed:zoom-out-95", className )}
|
||||
{...props}
|
||||
>
|
||||
<SelectScrollUpButton />
|
||||
@@ -122,7 +122,7 @@ function SelectItem({
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<SelectPrimitive.ItemText className="flex flex-1 shrink-0 gap-2 whitespace-nowrap">
|
||||
<SelectPrimitive.ItemText className="flex flex-1 shrink-0 gap-2 whitespace-normal break-words">
|
||||
{children}
|
||||
</SelectPrimitive.ItemText>
|
||||
<SelectPrimitive.ItemIndicator
|
||||
|
||||
@@ -71,6 +71,9 @@ export interface ChannelRef {
|
||||
channelName?: string | null;
|
||||
threadId?: string | null;
|
||||
threadName?: string | null;
|
||||
/** Channel topic (captured in gateway metadata.channel.topic). */
|
||||
topic?: string | null;
|
||||
nsfw?: boolean;
|
||||
}
|
||||
|
||||
export interface ReferenceInfo {
|
||||
|
||||
Reference in New Issue
Block a user