Enable bounded proximity conversations and confirmed social memories

This commit is contained in:
Codex
2026-09-22 17:10:15 -04:00
parent ce6b38f3bb
commit ee8161eca2
5 changed files with 111 additions and 9 deletions
+28 -1
View File
@@ -1,6 +1,7 @@
export type ConversationChannel='say'|'whisper';
export type ConversationRequest={
socialTurn?:0|1;
id:string;
createdAt:string;
speakerName:string;
@@ -79,7 +80,7 @@ export async function leaseConversation(db:D1Database):Promise<ConversationReque
.bind(staleBefore).run();
const row=await db.prepare(`SELECT id,created_at,speaker_name,message,channel,zone_name,receiver,selected_player,nearby_bots,status,attempts
FROM conversation_requests WHERE status='pending' ORDER BY created_at LIMIT 1`).first<any>();
FROM conversation_requests WHERE status='pending' ORDER BY CASE WHEN channel LIKE 'social-%' THEN 1 ELSE 0 END,created_at LIMIT 1`).first<any>();
if(!row)return null;
const startedAt=new Date().toISOString();
@@ -89,6 +90,7 @@ export async function leaseConversation(db:D1Database):Promise<ConversationReque
if(updated.meta.changes!==1)return null;
return {
socialTurn:row.channel==='social-start'?0:row.channel==='social-reply'?1:undefined,
id:row.id,
createdAt:row.created_at,
speakerName:row.speaker_name,
@@ -141,3 +143,28 @@ export async function pendingDeliveries(db:D1Database){
}
return deliveries;
}
// One autonomous exchange per two-minute bucket; human chat keeps priority.
export async function queueSocialEncounter(db:D1Database,payload:Record<string,unknown>){
const a=text(payload,'socialA',40),b=text(payload,'socialB',40);
const at=Number(payload.observedAt);
if(!a||!b||a===b||!Number.isFinite(at)||Math.abs(Date.now()/1000-at)>60)return;
await ensure(db);
const now=new Date().toISOString();
const cutoff=new Date(Date.now()-10*60_000).toISOString();
const id='social-'+Math.floor(Date.now()/120_000);
await db.prepare(`INSERT OR IGNORE INTO conversation_requests
(id,created_at,speaker_name,message,channel,zone_name,receiver,selected_player,nearby_bots,status,attempts)
SELECT ?,?,?,?,'social-start',?,?,?,?,'pending',0
WHERE NOT EXISTS (SELECT 1 FROM conversation_requests WHERE status IN ('pending','processing','awaiting_delivery'))
AND NOT EXISTS (SELECT 1 FROM conversation_requests WHERE channel LIKE 'social-%' AND created_at>? AND (speaker_name IN (?,?) OR selected_player IN (?,?)))`)
.bind(id,now,b,'Initiate a brief friendly in-world conversation with your nearby companion.',text(payload,'zoneName',120),a,a,b,cutoff,a,b,a,b).run();
}
export async function queueSocialReply(db:D1Database,request:ConversationRequest,botName:string,reply:string){
if(request.socialTurn!==0)return;
await db.prepare(`INSERT OR IGNORE INTO conversation_requests
(id,created_at,speaker_name,message,channel,zone_name,receiver,selected_player,nearby_bots,status,attempts)
VALUES (?,?,?,?,'social-reply',?,?,?,?,'pending',0)`)
.bind(request.id+':reply',new Date().toISOString(),botName,reply,request.zoneName,request.speakerName,request.speakerName,botName).run();
}