Add hybrid R4R media conversation mode
This commit is contained in:
1 parent
927636ec8b
commit
b73e305cf3
15 files changed
+357
-35
No files matched your search
@@ -34,8 +34,10 @@ export interface ConversationState {
|
||||
}
|
||||
|
||||
export const LEGACY_AUTOMATED_CONVERSATION_TURNS = 3;
|
||||
export const CONVERSATION_POLICY_VERSION = 1;
|
||||
export type ConversationPolicyMode = 'legacy' | 'r4r' | 'manual';
|
||||
export const DEFAULT_HYBRID_RAPPORT_TURNS = 3;
|
||||
export const CONVERSATION_POLICY_VERSION = 2;
|
||||
export type ConversationPolicyMode = 'legacy' | 'r4r' | 'r4r-media' | 'manual';
|
||||
export type HybridConversationPhase = 'rapport' | 'media-ask' | 'post-media-ask';
|
||||
|
||||
export type ConversationAutomationStatus =
|
||||
| 'active'
|
||||
@@ -48,6 +50,8 @@ export interface ConversationAutomationState {
|
||||
policyMode: ConversationPolicyMode;
|
||||
policyVersion: number;
|
||||
maxAutomatedTurns: number;
|
||||
rapportTurnsBeforeMediaAsk: number;
|
||||
hybridPhase: HybridConversationPhase | null;
|
||||
turnsUsed: number;
|
||||
status: ConversationAutomationStatus;
|
||||
lastIncomingKey: string | null;
|
||||
@@ -59,11 +63,14 @@ export interface ConversationAutomationState {
|
||||
export function createConversationAutomationState(
|
||||
policyMode: ConversationPolicyMode = 'legacy',
|
||||
maxAutomatedTurns = LEGACY_AUTOMATED_CONVERSATION_TURNS,
|
||||
rapportTurnsBeforeMediaAsk = DEFAULT_HYBRID_RAPPORT_TURNS,
|
||||
): ConversationAutomationState {
|
||||
return {
|
||||
policyMode,
|
||||
policyVersion: CONVERSATION_POLICY_VERSION,
|
||||
maxAutomatedTurns,
|
||||
rapportTurnsBeforeMediaAsk,
|
||||
hybridPhase: policyMode === 'r4r-media' ? 'rapport' : null,
|
||||
turnsUsed: 0,
|
||||
status: 'active',
|
||||
lastIncomingKey: null,
|
||||
@@ -205,6 +212,14 @@ export function isSafeR4RReply(message: string): boolean {
|
||||
&& !/\b(can|could|would|will) you\b[^.!?\n]{0,60}\b(pic|pics|photo|photos|selfie|selfies|video|videos|nude|nudes)\b/i.test(message);
|
||||
}
|
||||
|
||||
/** The hybrid policy gets one optional, non-explicit image request after rapport. */
|
||||
export function isSafeHybridMediaAsk(message: string): boolean {
|
||||
if (!isSafeConversationReply(message)) return false;
|
||||
if (!/\b(pic|pics|photo|photos|selfie|selfies|video|videos)\b/i.test(message)) return false;
|
||||
if (/\b(nude|nudes|naked|explicit|nsfw|sexy pic|sexual)\b/i.test(message)) return false;
|
||||
return !/\b(must|prove|dare|owe|have to|need to)\b/i.test(message);
|
||||
}
|
||||
|
||||
export function serializeConversationState(state: ConversationState): string {
|
||||
return JSON.stringify(state);
|
||||
}
|
||||
@@ -238,7 +253,9 @@ export function parseConversationState(value: string | null | undefined): Conver
|
||||
function parseAutomationState(value: unknown): ConversationAutomationState {
|
||||
if (!value || typeof value !== 'object') return createConversationAutomationState();
|
||||
const parsed = value as Partial<ConversationAutomationState>;
|
||||
const policyMode: ConversationPolicyMode = parsed.policyMode === 'r4r' || parsed.policyMode === 'manual'
|
||||
const policyMode: ConversationPolicyMode = parsed.policyMode === 'r4r'
|
||||
|| parsed.policyMode === 'r4r-media'
|
||||
|| parsed.policyMode === 'manual'
|
||||
? parsed.policyMode
|
||||
: 'legacy';
|
||||
const maxAutomatedTurns = typeof parsed.maxAutomatedTurns === 'number'
|
||||
@@ -248,12 +265,22 @@ function parseAutomationState(value: unknown): ConversationAutomationState {
|
||||
parsed.status === 'image-received' || parsed.status === 'human-required' || parsed.status === 'boundary' || parsed.status === 'user-overridden'
|
||||
? parsed.status
|
||||
: 'active';
|
||||
const rapportTurnsBeforeMediaAsk = typeof parsed.rapportTurnsBeforeMediaAsk === 'number'
|
||||
? Math.max(1, Math.min(19, Math.trunc(parsed.rapportTurnsBeforeMediaAsk)))
|
||||
: DEFAULT_HYBRID_RAPPORT_TURNS;
|
||||
const hybridPhase: HybridConversationPhase | null = policyMode !== 'r4r-media'
|
||||
? null
|
||||
: parsed.hybridPhase === 'media-ask' || parsed.hybridPhase === 'post-media-ask'
|
||||
? parsed.hybridPhase
|
||||
: 'rapport';
|
||||
return {
|
||||
policyMode,
|
||||
policyVersion: typeof parsed.policyVersion === 'number' && parsed.policyVersion > 0
|
||||
? Math.trunc(parsed.policyVersion)
|
||||
: CONVERSATION_POLICY_VERSION,
|
||||
maxAutomatedTurns,
|
||||
rapportTurnsBeforeMediaAsk,
|
||||
hybridPhase,
|
||||
turnsUsed: typeof parsed.turnsUsed === 'number'
|
||||
? Math.max(0, Math.min(maxAutomatedTurns, Math.trunc(parsed.turnsUsed)))
|
||||
: 0,
|
||||
|
||||
Reference in new issue
Block a user