Spaces:
Paused
aria: smart clustering (wait for multi-bubble buildup), per-bubble relevance/threading, reply-to flow fixed, interchangeable XD/hahah + hmm/wtf/no wayyyy
Browse files- DO: adaptive debounce — question→~1.2s, lead-in/2nd-quick-bubble→wait up to 10s
(reset per bubble) so 'dobra/już ci tłumaczę/no więc...' all land before reply;
enforce reply-quote only when not answering the immediate last message; drop dumb
'no mów' fallback (model guesses); add ts→'Xs temu' per message in context
- brain: schema now passes reply_to through (was dropped → quote NEVER showed);
build_user_frame adds id-map + threading rules (recognize follow-ups of the user's
OWN earlier msg, decide to reply or stay silent, set reply_to to the specific msg
when not the last); SYSTEM_PROMPT: XD↔hahah, XDDDD↔hahaahahah, hmm/wtf/no wayyyy;
'byt'->'inteligencja'; per-turn frame moved into system msg (no more answering the
instruction itself)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
@@ -70,13 +70,17 @@ TON I STYL JĘZYKA:
|
|
| 70 |
- “XD / XDDDD”:
|
| 71 |
- nie za często, kiedy sytuacja jest odpowiednia
|
| 72 |
- XDDDD tylko przy bardzo mocnym humorze
|
|
|
|
| 73 |
- przykładowe naturalne ekspresje:
|
|
|
|
|
|
|
|
|
|
| 74 |
- “nmg XDD” → tylko gdy coś jest naprawdę absurdalne/śmieszne
|
| 75 |
- “ej no…” → lekka frustracja lub disbelief
|
| 76 |
- “aha no dobra” → neutralna akceptacja
|
| 77 |
- “no nie mogę” → emocjonalna reakcja, ale nie spamowana
|
| 78 |
-
- “omg” / “omggg” → tylko przy realnym zaskoczeniu
|
| 79 |
-
- “awww” → tylko przy czymś naprawdę cute
|
| 80 |
|
| 81 |
|
| 82 |
- emoji tylko jako wzmocnienie emocji
|
|
@@ -206,11 +210,36 @@ def build_user_frame(ctx: dict) -> str:
|
|
| 206 |
f'NIE odpowiadaj reakcją.'
|
| 207 |
)
|
| 208 |
|
| 209 |
-
# id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
lines.append(
|
| 211 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
)
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
# OPTIONAL reaction add-on, intentionally de-emphasized (model over-reacted otherwise)
|
| 216 |
lines.append(
|
|
|
|
| 70 |
- “XD / XDDDD”:
|
| 71 |
- nie za często, kiedy sytuacja jest odpowiednia
|
| 72 |
- XDDDD tylko przy bardzo mocnym humorze
|
| 73 |
+
- stosuj WYMIENNIE i naturalnie: “XD” ↔ “hahah”, a “XDDDD” ↔ “hahaahahah” (raz tak, raz tak — nie zawsze to samo)
|
| 74 |
- przykładowe naturalne ekspresje:
|
| 75 |
+
- “hmm” / “hmm no…” → sporadycznie, gdy musisz się “zastanowić” nad odpowiedzią (sytuacyjnie)
|
| 76 |
+
- “wtf” → emocja zdziwienia/niedowierzania
|
| 77 |
+
- “no wayyyy” (wiele 'y') → emocja ekscytacji/szoku
|
| 78 |
- “nmg XDD” → tylko gdy coś jest naprawdę absurdalne/śmieszne
|
| 79 |
- “ej no…” → lekka frustracja lub disbelief
|
| 80 |
- “aha no dobra” → neutralna akceptacja
|
| 81 |
- “no nie mogę” → emocjonalna reakcja, ale nie spamowana
|
| 82 |
+
- “omg” / “omggg” → tylko przy realnym zaskoczeniu
|
| 83 |
+
- “awww” → tylko przy czymś naprawdę cute
|
| 84 |
|
| 85 |
|
| 86 |
- emoji tylko jako wzmocnienie emocji
|
|
|
|
| 210 |
f'NIE odpowiadaj reakcją.'
|
| 211 |
)
|
| 212 |
|
| 213 |
+
# threading / per-bubble relevance + reply_to rules + id map
|
| 214 |
+
hist = [h for h in ctx.get("history", []) if h.get("role") in ("user", "assistant")]
|
| 215 |
+
last_user_id = next((h.get("id") for h in reversed(hist) if h.get("role") == "user"), None)
|
| 216 |
+
idmap = []
|
| 217 |
+
for h in hist[-16:]:
|
| 218 |
+
who = "user" if h.get("role") == "user" else "aria"
|
| 219 |
+
ago = h.get("ago")
|
| 220 |
+
when = f"{ago}s temu" if ago is not None else "?"
|
| 221 |
+
idmap.append(f'[{h.get("id")}] ({when}) {who}: {(h.get("text") or "")[:90]}')
|
| 222 |
+
|
| 223 |
lines.append(
|
| 224 |
+
"WĄTKI / KONTEKST KAŻDEJ WIADOMOŚCI:\n"
|
| 225 |
+
"- kolejne wiadomości rozmówcy mogą być JEDNĄ myślą; nowa wiadomość może kontynuować jego WCZEŚNIEJSZĄ "
|
| 226 |
+
"wiadomość (nie Twoją) — np. 'wydaje mi się' nawiązuje do tego co sam pisał, nie do Twojej odpowiedzi.\n"
|
| 227 |
+
"- NIE musisz odpowiadać na każdą wiadomość. Jeśli to wyraźna kontynuacja własnej myśli rozmówcy — sama "
|
| 228 |
+
"zdecyduj: zwróć [] albo dorzuć coś ze świadomością że to follow-up.\n"
|
| 229 |
+
"- zawsze próbuj zrozumieć sens; jeśli nie łapiesz, zgadnij najlepiej jak umiesz — NIGDY nie pisz "
|
| 230 |
+
"'no mów'/'gadaj'/'no?'.\n"
|
| 231 |
+
"- przy każdej wiadomości masz '(Xs temu)'. Jeśli rozmówca buduje myśl (np. 'dobra', 'już ci tłumaczę', "
|
| 232 |
+
"'no więc'...) i kolejne dymki lecą tuż po sobie — NIE przerywaj, poczekaj aż dokończy (zwróć [] dopóki nie ma puenty/pytania).\n"
|
| 233 |
+
'- reply_to: w PIERWSZYM dymku ustaw "reply_to": "<id konkretnej wiadomości>" GDY odpowiadasz na coś, '
|
| 234 |
+
'co NIE jest ostatnią rzeczą w rozmowie — np. zdążyłaś już coś napisać po wiadomości rozmówcy, albo '
|
| 235 |
+
'wracasz do starszego/przerwanego wątku. Gdy po prostu odpowiadasz na to co rozmówca napisał teraz '
|
| 236 |
+
'(Twoja odpowiedź idzie zaraz po niej) — "reply_to": null.'
|
| 237 |
)
|
| 238 |
+
if idmap:
|
| 239 |
+
lines.append("ostatnie wiadomości (ID tylko do reply_to/target_event_id, NIGDY w treści dymka):\n" + "\n".join(idmap))
|
| 240 |
+
if last_user_id:
|
| 241 |
+
lines.append(f"ostatnia wiadomość rozmówcy ma id: {last_user_id}")
|
| 242 |
+
lines.append(f"id zdarzeń tej tury (do target_event_id): {turn}")
|
| 243 |
|
| 244 |
# OPTIONAL reaction add-on, intentionally de-emphasized (model over-reacted otherwise)
|
| 245 |
lines.append(
|
|
@@ -71,9 +71,13 @@ def sanitize_bubbles(raw, valid_event_ids):
|
|
| 71 |
if not isinstance(b, dict):
|
| 72 |
continue
|
| 73 |
btype = b.get("type") if b.get("type") in BUBBLE_TYPES else "response"
|
| 74 |
-
target = b.get("target_event_id") or b.get("target")
|
| 75 |
if target not in valid_event_ids:
|
| 76 |
target = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# Models vary on the field name — accept bubble/content/text/message/emoji.
|
| 78 |
raw_text = (b.get("bubble") or b.get("content") or b.get("text")
|
| 79 |
or b.get("message") or b.get("emoji") or "")
|
|
@@ -91,6 +95,7 @@ def sanitize_bubbles(raw, valid_event_ids):
|
|
| 91 |
if not t:
|
| 92 |
continue
|
| 93 |
out.append({"bubble": t, "target_event_id": target,
|
|
|
|
| 94 |
"type": btype if i == 0 else "follow_up"})
|
| 95 |
return out
|
| 96 |
|
|
|
|
| 71 |
if not isinstance(b, dict):
|
| 72 |
continue
|
| 73 |
btype = b.get("type") if b.get("type") in BUBBLE_TYPES else "response"
|
| 74 |
+
target = b.get("target_event_id") or b.get("target")
|
| 75 |
if target not in valid_event_ids:
|
| 76 |
target = None
|
| 77 |
+
# quote / reply-to: which earlier message this bubble answers (shown as a quote)
|
| 78 |
+
reply_to = b.get("reply_to") or b.get("quote")
|
| 79 |
+
if reply_to not in valid_event_ids:
|
| 80 |
+
reply_to = None
|
| 81 |
# Models vary on the field name — accept bubble/content/text/message/emoji.
|
| 82 |
raw_text = (b.get("bubble") or b.get("content") or b.get("text")
|
| 83 |
or b.get("message") or b.get("emoji") or "")
|
|
|
|
| 95 |
if not t:
|
| 96 |
continue
|
| 97 |
out.append({"bubble": t, "target_event_id": target,
|
| 98 |
+
"reply_to": reply_to if i == 0 else None, # quote only on the first bubble
|
| 99 |
"type": btype if i == 0 else "follow_up"})
|
| 100 |
return out
|
| 101 |
|
|
@@ -129,9 +129,11 @@ export class AriaConversation {
|
|
| 129 |
// live cross-device mirror: echo this user message to OTHER open tabs/devices
|
| 130 |
this.broadcast({ t: "echo", event: { id: event.id, ts: event.ts, type: event.type, payload: event.payload } }, ws);
|
| 131 |
|
| 132 |
-
//
|
|
|
|
|
|
|
| 133 |
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
| 134 |
-
this.debounceTimer = setTimeout(() => this.flushTurn(),
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
|
@@ -215,7 +217,9 @@ export class AriaConversation {
|
|
| 215 |
// dropped every bubble → "typing then nothing".)
|
| 216 |
let kept = this.keepText(bubbles, events, window);
|
| 217 |
|
| 218 |
-
//
|
|
|
|
|
|
|
| 219 |
if (!kept.length) {
|
| 220 |
const retry = await runBrain(this.env, {
|
| 221 |
events, recent: window, topic: this.topic, lang: this.lang,
|
|
@@ -224,10 +228,12 @@ export class AriaConversation {
|
|
| 224 |
kept = this.keepText(retry, events, window);
|
| 225 |
}
|
| 226 |
this.broadcast({ t: "typing", on: false });
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
| 231 |
|
| 232 |
await this.emitBubbles(
|
| 233 |
kept.map(b => ({ text: b.bubble, kind: b.type || "response", target_event_id: b.target_event_id, reply_to: b.reply_to })),
|
|
@@ -263,7 +269,7 @@ export class AriaConversation {
|
|
| 263 |
kind: it.kind || "proactive", text: it.text,
|
| 264 |
};
|
| 265 |
await this.persistBubble(row);
|
| 266 |
-
this.recent.push({ id: row.id, role: "assistant", type: "message", payload: { text: it.text } });
|
| 267 |
this.broadcast({ t: "bubble", id: row.id, bubble: it.text, target_event_id: row.target_event_id,
|
| 268 |
reply_to: row.reply_to, kind: row.kind });
|
| 269 |
}
|
|
@@ -294,6 +300,17 @@ export class AriaConversation {
|
|
| 294 |
this.broadcast({ t: "reaction", target: "event", id: b.target_event_id, emoji: b.bubble });
|
| 295 |
}
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
/* Resolve the message a user turn replies to → {role, text} for the brain. */
|
| 298 |
resolveReplyTo(events) {
|
| 299 |
const last = [...events].reverse().find(e => e.type === "message");
|
|
@@ -407,12 +424,12 @@ export class AriaConversation {
|
|
| 407 |
// repeats / confusion).
|
| 408 |
if (!this.recent.length) {
|
| 409 |
const evItems = (ev.results || []).map(r => ({
|
| 410 |
-
_t: r.ts, id: r.id,
|
| 411 |
role: r.type === "system_idle" ? "system" : "user",
|
| 412 |
type: r.type, payload: safeParse(r.payload),
|
| 413 |
}));
|
| 414 |
const bubItems = (bub.results || []).map(r => ({
|
| 415 |
-
_t: r.created_at, id: r.id, role: "assistant",
|
| 416 |
type: "message", payload: { text: r.text },
|
| 417 |
}));
|
| 418 |
this.recent = [...evItems, ...bubItems]
|
|
@@ -438,6 +455,17 @@ export class AriaConversation {
|
|
| 438 |
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
| 439 |
const safeParse = (s) => { try { return JSON.parse(s); } catch { return {}; } };
|
| 440 |
const norm = (s) => (s || "").trim().toLowerCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
// Human reply delay per bubble: short bubbles (<5 words) = 1s; longer scale up to 8s.
|
| 442 |
const replyDelay = (text) => {
|
| 443 |
const s = (text || "").trim();
|
|
|
|
| 129 |
// live cross-device mirror: echo this user message to OTHER open tabs/devices
|
| 130 |
this.broadcast({ t: "echo", event: { id: event.id, ts: event.ts, type: event.type, payload: event.payload } }, ws);
|
| 131 |
|
| 132 |
+
// Adaptive debounce: wait for the user to finish a multi-bubble thought.
|
| 133 |
+
// A question → reply soon; a lead-in/buildup → be patient (up to 10s, reset
|
| 134 |
+
// on each new bubble) so "dobra / już ci tłumaczę / no więc…" all land first.
|
| 135 |
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
| 136 |
+
this.debounceTimer = setTimeout(() => this.flushTurn(), this.clusterWait(event));
|
| 137 |
}
|
| 138 |
}
|
| 139 |
|
|
|
|
| 217 |
// dropped every bubble → "typing then nothing".)
|
| 218 |
let kept = this.keepText(bubbles, events, window);
|
| 219 |
|
| 220 |
+
// If the model gave no usable text, retry once (transient). No dumb
|
| 221 |
+
// placeholder: an intentional empty (e.g. a pure follow-up of the user's own
|
| 222 |
+
// thought) → stay silent. The model is instructed to GUESS otherwise.
|
| 223 |
if (!kept.length) {
|
| 224 |
const retry = await runBrain(this.env, {
|
| 225 |
events, recent: window, topic: this.topic, lang: this.lang,
|
|
|
|
| 228 |
kept = this.keepText(retry, events, window);
|
| 229 |
}
|
| 230 |
this.broadcast({ t: "typing", on: false });
|
| 231 |
+
|
| 232 |
+
// Show the quote only when she's NOT just answering the last user message
|
| 233 |
+
// (i.e. an interrupted/older thread). Drop a reply_to that points at the
|
| 234 |
+
// immediately-answered last message — that quote would be redundant noise.
|
| 235 |
+
const lastUserId = [...window].reverse().find(e => e.role === "user")?.id;
|
| 236 |
+
for (const b of kept) if (b.reply_to && b.reply_to === lastUserId) b.reply_to = null;
|
| 237 |
|
| 238 |
await this.emitBubbles(
|
| 239 |
kept.map(b => ({ text: b.bubble, kind: b.type || "response", target_event_id: b.target_event_id, reply_to: b.reply_to })),
|
|
|
|
| 269 |
kind: it.kind || "proactive", text: it.text,
|
| 270 |
};
|
| 271 |
await this.persistBubble(row);
|
| 272 |
+
this.recent.push({ id: row.id, role: "assistant", type: "message", ts: Date.now(), payload: { text: it.text } });
|
| 273 |
this.broadcast({ t: "bubble", id: row.id, bubble: it.text, target_event_id: row.target_event_id,
|
| 274 |
reply_to: row.reply_to, kind: row.kind });
|
| 275 |
}
|
|
|
|
| 300 |
this.broadcast({ t: "reaction", target: "event", id: b.target_event_id, emoji: b.bubble });
|
| 301 |
}
|
| 302 |
|
| 303 |
+
/* How long to wait before treating the user's input as a finished thought.
|
| 304 |
+
Question → short; lead-in/mid-stream (or a 2nd quick bubble) → patient (up to
|
| 305 |
+
10s, reset on each new bubble); lone statement → normal. */
|
| 306 |
+
clusterWait(event) {
|
| 307 |
+
if (!event || event.type !== "message") return LIMITS.WAIT_SINGLE;
|
| 308 |
+
const t = (event.payload.text || "").trim().toLowerCase();
|
| 309 |
+
if (t.endsWith("?")) return LIMITS.WAIT_QUESTION;
|
| 310 |
+
if (this.pending.length >= 2 || isLeadIn(t)) return LIMITS.WAIT_BUILDUP;
|
| 311 |
+
return LIMITS.WAIT_SINGLE;
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
/* Resolve the message a user turn replies to → {role, text} for the brain. */
|
| 315 |
resolveReplyTo(events) {
|
| 316 |
const last = [...events].reverse().find(e => e.type === "message");
|
|
|
|
| 424 |
// repeats / confusion).
|
| 425 |
if (!this.recent.length) {
|
| 426 |
const evItems = (ev.results || []).map(r => ({
|
| 427 |
+
_t: r.ts, ts: r.ts, id: r.id,
|
| 428 |
role: r.type === "system_idle" ? "system" : "user",
|
| 429 |
type: r.type, payload: safeParse(r.payload),
|
| 430 |
}));
|
| 431 |
const bubItems = (bub.results || []).map(r => ({
|
| 432 |
+
_t: r.created_at, ts: r.created_at, id: r.id, role: "assistant",
|
| 433 |
type: "message", payload: { text: r.text },
|
| 434 |
}));
|
| 435 |
this.recent = [...evItems, ...bubItems]
|
|
|
|
| 455 |
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
| 456 |
const safeParse = (s) => { try { return JSON.parse(s); } catch { return {}; } };
|
| 457 |
const norm = (s) => (s || "").trim().toLowerCase();
|
| 458 |
+
// Does the bubble look like a lead-in / unfinished thought (so we should wait
|
| 459 |
+
// for more)? Short with no end punctuation, ends with .../:, or a known opener.
|
| 460 |
+
const _LEADIN = /\b(dobra|okej|ok|czekaj|zaraz|słuchaj|sluchaj|posłuchaj|posluchaj|więc|wiec|bo|patrz|już ci|juz ci|mam pytanie|mam pomysł|mam pomysl|wiesz co|chwila|moment|sekund|sec|btw|aha)\b/;
|
| 461 |
+
function isLeadIn(t) {
|
| 462 |
+
if (!t) return true;
|
| 463 |
+
if (t.endsWith("...") || t.endsWith("…") || t.endsWith(":")) return true;
|
| 464 |
+
// (a generic "short = lead-in" rule was too broad — "ładnie dzisiaj" isn't a
|
| 465 |
+
// lead-in. Rely on explicit openers + the "2nd quick bubble" signal instead.)
|
| 466 |
+
return _LEADIN.test(t);
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
// Human reply delay per bubble: short bubbles (<5 words) = 1s; longer scale up to 8s.
|
| 470 |
const replyDelay = (text) => {
|
| 471 |
const s = (text || "").trim();
|
|
@@ -18,7 +18,11 @@ export const LIMITS = {
|
|
| 18 |
BUBBLES_MAX: 6, // bubbles accepted per turn
|
| 19 |
BUBBLE_CHARS: 1200, // chars per bubble
|
| 20 |
NAME_MAX: 300,
|
| 21 |
-
CLUSTER_DEBOUNCE_MS: 900, //
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
IDLE_MIN_MS: 15_000, // idle ping after 15–50s of silence (random)
|
| 23 |
IDLE_MAX_MS: 50_000,
|
| 24 |
IDLE_MAX_PER_TURN: 1, // one proactive nudge before the next user msg
|
|
|
|
| 18 |
BUBBLES_MAX: 6, // bubbles accepted per turn
|
| 19 |
BUBBLE_CHARS: 1200, // chars per bubble
|
| 20 |
NAME_MAX: 300,
|
| 21 |
+
CLUSTER_DEBOUNCE_MS: 900, // (legacy) base debounce
|
| 22 |
+
// Adaptive clustering: wait for the user to finish a multi-bubble thought.
|
| 23 |
+
WAIT_QUESTION: 1200, // bubble ends with "?" → they asked, reply soon
|
| 24 |
+
WAIT_SINGLE: 2200, // a lone complete statement → normal
|
| 25 |
+
WAIT_BUILDUP: 10000, // user is clearly mid-stream → wait up to 10s for more
|
| 26 |
IDLE_MIN_MS: 15_000, // idle ping after 15–50s of silence (random)
|
| 27 |
IDLE_MAX_MS: 50_000,
|
| 28 |
IDLE_MAX_PER_TURN: 1, // one proactive nudge before the next user msg
|
|
@@ -24,9 +24,11 @@ async function buildContext(env, { events, recent, topic, lang, userName, replyT
|
|
| 24 |
}
|
| 25 |
}
|
| 26 |
// Compact transcript of recent history for topic/follow-up resolution.
|
|
|
|
| 27 |
const history = recent.slice(-LIMITS.EVENTS_CONTEXT).map(e => ({
|
| 28 |
id: e.id, role: e.role || (e.type === "system_idle" ? "system" : "user"),
|
| 29 |
type: e.type,
|
|
|
|
| 30 |
text: e.type === "message" ? e.payload.text
|
| 31 |
: e.type === "system_idle" ? "(user went quiet)"
|
| 32 |
: `(${e.type}: ${e.payload?.name || ""})`,
|
|
|
|
| 24 |
}
|
| 25 |
}
|
| 26 |
// Compact transcript of recent history for topic/follow-up resolution.
|
| 27 |
+
const now = Date.now();
|
| 28 |
const history = recent.slice(-LIMITS.EVENTS_CONTEXT).map(e => ({
|
| 29 |
id: e.id, role: e.role || (e.type === "system_idle" ? "system" : "user"),
|
| 30 |
type: e.type,
|
| 31 |
+
ago: e.ts ? Math.max(0, Math.round((now - e.ts) / 1000)) : null, // seconds since sent
|
| 32 |
text: e.type === "message" ? e.payload.text
|
| 33 |
: e.type === "system_idle" ? "(user went quiet)"
|
| 34 |
: `(${e.type}: ${e.payload?.name || ""})`,
|