Add consent-gated media exchange library

This commit is contained in:
jjbrunton committed 2026-08-09 19:24:38 +01:00
1 parent b8e4a1bb88
commit bf474dfe10
42 files changed
+2533 -9

No files matched your search

+75
View File
@@ -1,4 +1,5 @@
import OpenAI from 'openai';
import { z } from 'zod';
import type { Config } from '../config/index.js';
import type { Logger } from '../utils/logger.js';
import type { AiUsageRecordInput, AiUsageOutcome } from '../database/ai-usage-repository.js';
@@ -10,6 +11,10 @@ import {
type ConversationState,
} from '../services/conversation-state.js';
import { isSafeOutboundMessage } from '../services/outbound-message-safety.js';
import {
MEDIA_LIBRARY_TAGS,
type MediaLibraryClassification,
} from '../services/media-library-types.js';
export type DetectedGender = 'male' | 'female' | 'other' | 'unknown';
@@ -142,6 +147,61 @@ export class OpenRouterClient {
throw new Error('AI_REFUSAL: All models refused to analyze image');
}
async classifyLibraryImage(imageBase64: string, mimeType: string): Promise<MediaLibraryClassification> {
const models = [
this.config.openrouter.imageAnalysisModel,
...this.config.openrouter.imageAnalysisFallbackModels,
];
let lastError: unknown = null;
for (const model of models) {
const startedAt = Date.now();
try {
const response = await this.client.chat.completions.create({
model,
messages: [{
role: 'user',
content: [
{
type: 'text',
text: `Classify this operator-owned adult image for a private media library. Return JSON only with this exact shape:
{"tags":["selfie"],"safety":"sfw","description":"short neutral description","confidence":0.95}
Allowed tags: ${MEDIA_LIBRARY_TAGS.join(', ')}.
- selfie: a face-led or self-portrait style image
- chest: a clothed chest or upper torso
- topless: a bare chest or bare upper torso
- penis: a visible penis
- feet: feet are a material focus
Use every applicable tag; tags may overlap. Set safety to "nsfw" when nudity, exposed genitals, or clearly sexual presentation is visible, otherwise "sfw". Confidence must be between 0 and 1. Do not identify the person, infer age, or add attributes outside this taxonomy.`,
},
{
type: 'image_url',
image_url: { url: `data:${mimeType};base64,${imageBase64}` },
},
],
}],
max_tokens: 180,
temperature: 0,
});
const content = response.choices[0]?.message?.content ?? '';
const parsed = MediaLibraryClassificationSchema.parse(parseJsonObject(content));
this.recordUsage('classifyLibraryImage', model, startedAt, response, 'success');
return {
tags: [...new Set(parsed.tags)],
safety: parsed.safety,
description: parsed.description.trim().slice(0, 300),
confidence: parsed.confidence,
};
} catch (error) {
lastError = error;
this.recordError('classifyLibraryImage', model, startedAt, error);
this.logger.warn(`[AI] classifyLibraryImage: ${model} failed${model !== models.at(-1) ? ', trying fallback...' : ''}`);
}
}
throw lastError ?? new Error('Library image classification failed');
}
private async tryAnalyzeWithModel(model: string, imageBase64: string, mimeType: string): Promise<ImageAnalysisResult | null> {
const startedAt = Date.now();
const genderPrompt = `First, identify the gender of the person in this image. Respond with EXACTLY one of these on the first line: GENDER:male, GENDER:female, GENDER:other, or GENDER:unknown
@@ -684,3 +744,18 @@ Just write the message, nothing else.`;
function numericUsage(value: unknown): number {
return typeof value === 'number' && Number.isFinite(value) ? Math.max(0, Math.trunc(value)) : 0;
}
const MediaLibraryClassificationSchema = z.strictObject({
tags: z.array(z.enum(MEDIA_LIBRARY_TAGS)).min(1),
safety: z.enum(['sfw', 'nsfw']),
description: z.string().min(1).max(500),
confidence: z.number().min(0).max(1),
});
function parseJsonObject(text: string): unknown {
const cleaned = text.trim().replace(/^```(?:json)?\s*/i, '').replace(/\s*```$/, '');
const start = cleaned.indexOf('{');
const end = cleaned.lastIndexOf('}');
if (start < 0 || end < start) throw new Error('Classifier returned no JSON object');
return JSON.parse(cleaned.slice(start, end + 1));
}