Spaces:
Paused
Paused
Malina Claude Opus 4.8 commited on
Commit ·
1308d69
1
Parent(s): f535a3a
aria: fix role mixup in rehydrated history; reliable language-matched idle nudges; anti-echo
Browse files- DO: rehydrate context by MERGING user events + ARIA bubbles in time order with
correct roles (was loading only user events -> model couldn't tell who said what)
- idle nudges generated locally from PL/EN pools, language = user's LAST message,
no-repeat; always sends (model sometimes returned nothing)
- drop bubbles that echo the user or repeat a prior line
- persona: explicit role note + 'never copy the user'; signoff lang = last user msg
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
- brain/orchestrator.py +2 -2
- brain/persona.py +2 -0
- cloudflare/aria-worker/src/conversation-do.js +72 -24
brain/orchestrator.py
CHANGED
|
@@ -50,8 +50,8 @@ def _signoff_bubbles(ctx: dict):
|
|
| 50 |
"""When inference fails, ARIA bails like a person — two casual bubbles, randomized,
|
| 51 |
language-matched (Polish by default)."""
|
| 52 |
import random
|
| 53 |
-
|
| 54 |
-
|
| 55 |
has_pl = any(c in txt for c in "ąćęłńóśźż") or any(
|
| 56 |
w in txt for w in [" nie", "cześć", "jesteś", " co ", " tak", "dzię", " jak"])
|
| 57 |
has_en = any(w in txt for w in [" the ", " you", " do ", "speak", "what",
|
|
|
|
| 50 |
"""When inference fails, ARIA bails like a person — two casual bubbles, randomized,
|
| 51 |
language-matched (Polish by default)."""
|
| 52 |
import random
|
| 53 |
+
users = [h.get("text", "") for h in ctx.get("history", []) if h.get("role") == "user"]
|
| 54 |
+
txt = (users[-1] if users else "").lower() # language of the LAST user message
|
| 55 |
has_pl = any(c in txt for c in "ąćęłńóśźż") or any(
|
| 56 |
w in txt for w in [" nie", "cześć", "jesteś", " co ", " tak", "dzię", " jak"])
|
| 57 |
has_en = any(w in txt for w in [" the ", " you", " do ", "speak", "what",
|
brain/persona.py
CHANGED
|
@@ -24,6 +24,8 @@ JĘZYK:
|
|
| 24 |
- domyślnie polski. Jeśli user pisze w innym języku, odpowiadasz w jego języku.
|
| 25 |
|
| 26 |
KONTEKST:
|
|
|
|
|
|
|
| 27 |
- wiadomości usera mogą się łączyć: "kup jabłka" a zaraz "i gruszki" to JEDEN wątek. Łącz je.
|
| 28 |
- obrazek bez tekstu? dopytaj krótko, np. "co to?" albo "i co z tym?".
|
| 29 |
|
|
|
|
| 24 |
- domyślnie polski. Jeśli user pisze w innym języku, odpowiadasz w jego języku.
|
| 25 |
|
| 26 |
KONTEKST:
|
| 27 |
+
- w historii role są oznaczone: "user" to druga osoba, "assistant" to TY (ARIA). Nie myl tego.
|
| 28 |
+
- NIGDY nie powtarzaj słowo w słowo wiadomości usera ani swojej poprzedniej wiadomości. Odpowiadaj, nie kopiuj.
|
| 29 |
- wiadomości usera mogą się łączyć: "kup jabłka" a zaraz "i gruszki" to JEDEN wątek. Łącz je.
|
| 30 |
- obrazek bez tekstu? dopytaj krótko, np. "co to?" albo "i co z tym?".
|
| 31 |
|
cloudflare/aria-worker/src/conversation-do.js
CHANGED
|
@@ -100,31 +100,36 @@ export class AriaConversation {
|
|
| 100 |
const events = systemEvent ? [systemEvent] : this.pending.splice(0);
|
| 101 |
if (!events.length) return;
|
| 102 |
this.busy = true;
|
| 103 |
-
|
| 104 |
-
// Idle nudge: put the system_idle event INTO the history so the brain knows
|
| 105 |
-
// the user went quiet (otherwise it just re-answers the last message — the
|
| 106 |
-
// "duplicate reply" bug). Remember the last thing ARIA said, to dedup against.
|
| 107 |
-
const lastAssistant = [...this.recent].reverse().find(e => e.role === "assistant");
|
| 108 |
-
if (systemEvent) {
|
| 109 |
-
this.recent.push({ id: systemEvent.id, role: "system",
|
| 110 |
-
type: "system_idle", payload: systemEvent.payload });
|
| 111 |
-
}
|
| 112 |
-
|
| 113 |
this.broadcast({ t: "typing", on: true });
|
| 114 |
|
| 115 |
try {
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
| 126 |
}
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
this.broadcast({ t: "typing", on: false });
|
| 129 |
|
| 130 |
for (const b of bubbles) {
|
|
@@ -169,11 +174,25 @@ export class AriaConversation {
|
|
| 169 |
const since = Date.now();
|
| 170 |
this.idleTimer = setTimeout(() => {
|
| 171 |
this.idleCount++;
|
| 172 |
-
this.flushTurn(idleEvent(since)); //
|
| 173 |
}, LIMITS.IDLE_MS);
|
| 174 |
}
|
| 175 |
clearIdle() { if (this.idleTimer) { clearTimeout(this.idleTimer); this.idleTimer = null; } }
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
trimRecent() {
|
| 178 |
if (this.recent.length > LIMITS.EVENTS_CONTEXT * 2)
|
| 179 |
this.recent = this.recent.slice(-LIMITS.EVENTS_CONTEXT * 2);
|
|
@@ -216,12 +235,23 @@ export class AriaConversation {
|
|
| 216 |
const bub = await this.env.DB.prepare(
|
| 217 |
"SELECT id, target_event_id, kind, text, created_at FROM aria_bubbles WHERE conversation_id = ? ORDER BY created_at ASC LIMIT 200"
|
| 218 |
).bind(this.cid).all().catch(() => ({ results: [] }));
|
| 219 |
-
//
|
|
|
|
|
|
|
|
|
|
| 220 |
if (!this.recent.length) {
|
| 221 |
-
|
| 222 |
-
|
|
|
|
| 223 |
type: r.type, payload: safeParse(r.payload),
|
| 224 |
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
}
|
| 226 |
this.send(ws, {
|
| 227 |
t: "history",
|
|
@@ -241,3 +271,21 @@ export class AriaConversation {
|
|
| 241 |
|
| 242 |
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
| 243 |
const safeParse = (s) => { try { return JSON.parse(s); } catch { return {}; } };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
const events = systemEvent ? [systemEvent] : this.pending.splice(0);
|
| 101 |
if (!events.length) return;
|
| 102 |
this.busy = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
this.broadcast({ t: "typing", on: true });
|
| 104 |
|
| 105 |
try {
|
| 106 |
+
let bubbles;
|
| 107 |
+
if (systemEvent) {
|
| 108 |
+
// Idle nudge generated locally — reliable + free + language-matched to the
|
| 109 |
+
// user's LAST message (the model sometimes returned nothing/echo here).
|
| 110 |
+
await sleep(800);
|
| 111 |
+
bubbles = this.idleNudge();
|
| 112 |
+
} else {
|
| 113 |
+
const validEventIds = new Set(this.recent.map(e => e.id));
|
| 114 |
+
bubbles = await runBrain(this.env, {
|
| 115 |
+
events, recent: this.recent, topic: this.topic,
|
| 116 |
+
lang: this.lang, validEventIds,
|
| 117 |
+
});
|
| 118 |
}
|
| 119 |
|
| 120 |
+
// Drop bubbles that just echo the user's message or repeat an earlier line
|
| 121 |
+
// (the "ARIA repeats the user" bug), and de-dupe within this batch.
|
| 122 |
+
const said = new Set(this.recent
|
| 123 |
+
.filter(e => e.type === "message")
|
| 124 |
+
.map(e => norm(e.payload.text)));
|
| 125 |
+
const seen = new Set();
|
| 126 |
+
bubbles = bubbles.filter(b => {
|
| 127 |
+
const t = norm(b.bubble);
|
| 128 |
+
if (!t || said.has(t) || seen.has(t)) return false;
|
| 129 |
+
seen.add(t);
|
| 130 |
+
return true;
|
| 131 |
+
});
|
| 132 |
+
|
| 133 |
this.broadcast({ t: "typing", on: false });
|
| 134 |
|
| 135 |
for (const b of bubbles) {
|
|
|
|
| 174 |
const since = Date.now();
|
| 175 |
this.idleTimer = setTimeout(() => {
|
| 176 |
this.idleCount++;
|
| 177 |
+
this.flushTurn(idleEvent(since)); // emit a local nudge (see idleNudge)
|
| 178 |
}, LIMITS.IDLE_MS);
|
| 179 |
}
|
| 180 |
clearIdle() { if (this.idleTimer) { clearTimeout(this.idleTimer); this.idleTimer = null; } }
|
| 181 |
|
| 182 |
+
/* One short check-in ping, in the language of the user's LAST message, not
|
| 183 |
+
repeating the previous nudge. Always returns exactly one proactive bubble. */
|
| 184 |
+
idleNudge() {
|
| 185 |
+
const lastUser = [...this.recent].reverse()
|
| 186 |
+
.find(e => e.role === "user" && e.type === "message");
|
| 187 |
+
const lang = detectLang(lastUser ? (lastUser.payload.text || "") : "");
|
| 188 |
+
const pool = lang === "en" ? IDLE_EN : IDLE_PL;
|
| 189 |
+
const opts = pool.filter(x => x.toLowerCase() !== (this.lastNudge || "").toLowerCase());
|
| 190 |
+
const from = opts.length ? opts : pool;
|
| 191 |
+
const pick = from[Math.floor(Math.random() * from.length)];
|
| 192 |
+
this.lastNudge = pick;
|
| 193 |
+
return [{ bubble: pick, target_event_id: null, type: "proactive" }];
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
trimRecent() {
|
| 197 |
if (this.recent.length > LIMITS.EVENTS_CONTEXT * 2)
|
| 198 |
this.recent = this.recent.slice(-LIMITS.EVENTS_CONTEXT * 2);
|
|
|
|
| 235 |
const bub = await this.env.DB.prepare(
|
| 236 |
"SELECT id, target_event_id, kind, text, created_at FROM aria_bubbles WHERE conversation_id = ? ORDER BY created_at ASC LIMIT 200"
|
| 237 |
).bind(this.cid).all().catch(() => ({ results: [] }));
|
| 238 |
+
// Rehydrate rolling context — MERGE user events AND ARIA's bubbles in time
|
| 239 |
+
// order with correct roles. Previously only user events were loaded, so after a
|
| 240 |
+
// reconnect the model couldn't tell its own messages from the user's (caused
|
| 241 |
+
// repeats / confusion).
|
| 242 |
if (!this.recent.length) {
|
| 243 |
+
const evItems = (ev.results || []).map(r => ({
|
| 244 |
+
_t: r.ts, id: r.id,
|
| 245 |
+
role: r.type === "system_idle" ? "system" : "user",
|
| 246 |
type: r.type, payload: safeParse(r.payload),
|
| 247 |
}));
|
| 248 |
+
const bubItems = (bub.results || []).map(r => ({
|
| 249 |
+
_t: r.created_at, id: r.id, role: "assistant",
|
| 250 |
+
type: "message", payload: { text: r.text },
|
| 251 |
+
}));
|
| 252 |
+
this.recent = [...evItems, ...bubItems]
|
| 253 |
+
.sort((a, z) => a._t - z._t)
|
| 254 |
+
.map(({ _t, ...rest }) => rest);
|
| 255 |
}
|
| 256 |
this.send(ws, {
|
| 257 |
t: "history",
|
|
|
|
| 271 |
|
| 272 |
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
| 273 |
const safeParse = (s) => { try { return JSON.parse(s); } catch { return {}; } };
|
| 274 |
+
const norm = (s) => (s || "").trim().toLowerCase();
|
| 275 |
+
|
| 276 |
+
// Idle check-in pings (no trailing period, lowercase — messenger style).
|
| 277 |
+
const IDLE_PL = ["halo?", "jesteś?", "ej żyjesz?", "no i?", "halo halo?", "śpisz?",
|
| 278 |
+
"jesteś tam?", "ej?", "no co tam?", "haloo?", "wróciłeś?", "ej halo?"];
|
| 279 |
+
const IDLE_EN = ["you there?", "hello?", "still there?", "you alive?", "helloo?",
|
| 280 |
+
"u there?", "yo?", "still with me?", "hey?", "anyone there?", "you back?", "hey you there?"];
|
| 281 |
+
|
| 282 |
+
// Guess the language of a piece of text: Polish if it has Polish diacritics or
|
| 283 |
+
// common Polish words, English if it has English markers; Polish by default.
|
| 284 |
+
function detectLang(text) {
|
| 285 |
+
const t = (text || "").toLowerCase();
|
| 286 |
+
if (/[ąćęłńóśźż]/.test(t)) return "pl";
|
| 287 |
+
const pl = /\b(nie|tak|cześć|czesc|jesteś|jestes|co|jak|dzięki|dzieki|gdzie|czemu|jestem|mam|robisz|siema|elo|dobra|spoko|kup|gruszki)\b/.test(t);
|
| 288 |
+
const en = /\b(the|you|do|are|what|hello|hi|can|yeah|yes|how|where|why|im|hey|your|with|me|good|thanks)\b/.test(t);
|
| 289 |
+
if (en && !pl) return "en";
|
| 290 |
+
return "pl"; // Polish-first
|
| 291 |
+
}
|