Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

"Wrong bot?": correct and resend the message the reply answers

Ada (kwant bot)c6af8d529fd9
diff --git a/frontend/src/routeChoice.ts b/frontend/src/routeChoice.ts
index daafd466..ff2d38d2 100644
--- a/frontend/src/routeChoice.ts
+++ b/frontend/src/routeChoice.ts
@@ -134,15 +134,36 @@ export function isAskingReply(body: unknown): body is { status: 'asking'; route_
return !!body && typeof body === 'object' && (body as { status?: unknown }).status === 'asking';
}
-/** For each bot reply, the person's latest saved channel message before it: the message "Wrong bot?"
- * resends. Replies don't carry the message id they answer, so this is the nearest earlier one.
- * Optimistic messages (temp ids) aren't saved yet and can't be corrected. */
-export function questionsOfReplies<M extends { id: string; sender_role: string; thread_id?: string | null }>(messages: M[]): Map<string, M> {
+/** The id of the person's message a routed reply answers (`meta.routing.message_id`); null on
+ * @mentions, DMs and replies saved before the server recorded it. */
+export function answeredMessageId(meta: string | null | undefined): string | null {
+ if (!meta) return null;
+ try {
+ const id = JSON.parse(meta)?.routing?.message_id;
+ return typeof id === 'string' && id ? id : null;
+ } catch {
+ return null;
+ }
+}
+
+/** For each bot reply, the person's message "Wrong bot?" corrects and resends: the one its routing
+ * says it answers, even when the person wrote more since. Older replies without that id fall back
+ * to the person's latest saved channel message before them. Thread and optimistic (temp id)
+ * messages are never picked; a recorded id that isn't loaded gets no "Wrong bot?" rather than a guess. */
+export function questionsOfReplies<M extends { id: string; sender_role: string; thread_id?: string | null; meta?: string | null }>(messages: M[]): Map<string, M> {
const out = new Map<string, M>();
+ const byId = new Map<string, M>();
let last: M | null = null;
for (const m of messages) {
- if (m.sender_role === 'user') last = m.id.startsWith('temp') || m.thread_id ? null : m;
- else if (m.sender_role === 'kwant' && last) out.set(m.id, last);
+ if (m.sender_role === 'user') {
+ const saved = !m.id.startsWith('temp') && !m.thread_id;
+ last = saved ? m : null;
+ if (saved) byId.set(m.id, m);
+ } else if (m.sender_role === 'kwant') {
+ const answered = answeredMessageId(m.meta);
+ const q = answered ? byId.get(answered) : last;
+ if (q) out.set(m.id, q);
+ }
}
return out;
}
diff --git a/frontend/tests/routerUi.test.mjs b/frontend/tests/routerUi.test.mjs
index 956d6890..f074eadd 100644
--- a/frontend/tests/routerUi.test.mjs
+++ b/frontend/tests/routerUi.test.mjs
@@ -165,7 +165,7 @@ test('send: "asking" means a card is waiting, not an error', () => {
assert.equal(m.isAskingReply(null), false);
});
-test('wrong bot: each reply resends the person\'s latest saved message before it, not thread or unsaved ones', () => {
+test('wrong bot: a reply without routing.message_id falls back to the person\'s latest saved message before it, not thread or unsaved ones', () => {
const q = m.questionsOfReplies([
{ id: 'u1', sender_role: 'user', text: 'corn dog recipe' },
{ id: 'r1', sender_role: 'kwant' },
@@ -191,6 +191,37 @@ test('wrong bot: each reply resends the person\'s latest saved message before it
assert.deepEqual(m.rerouteTargetsFor(null, kwants), []);
});
+const routed = (id) => JSON.stringify({ routing: { kwant_name: 'Chef', basis: 'keywords', message_id: id } });
+
+test('wrong bot: routing.message_id picks that exact message, even with a newer one before the reply', () => {
+ const q = m.questionsOfReplies([
+ { id: 'u1', sender_role: 'user', text: 'corn dog recipe' },
+ { id: 'u2', sender_role: 'user', text: 'also, what wine goes with it?' },
+ { id: 'r1', sender_role: 'kwant', meta: routed('u1') },
+ { id: 'r2', sender_role: 'kwant', meta: routed('u2') },
+ { id: 'r3', sender_role: 'kwant', meta: JSON.stringify({ routing: { kwant_name: 'Chef' } }) },
+ ]);
+ assert.equal(q.get('r1').text, 'corn dog recipe');
+ assert.equal(q.get('r2').text, 'also, what wine goes with it?');
+ // No id (a reply saved before the server recorded it): the nearest earlier message
+ assert.equal(q.get('r3').id, 'u2');
+ assert.equal(m.answeredMessageId(routed('u1')), 'u1');
+ assert.equal(m.answeredMessageId('{"routing":{}}'), null);
+ assert.equal(m.answeredMessageId('not json'), null);
+ assert.equal(m.answeredMessageId(null), null);
+});
+
+test('wrong bot: a recorded id that isn\'t loaded, or is a thread message, gets no "Wrong bot?" instead of a guess', () => {
+ const q = m.questionsOfReplies([
+ { id: 'u2', sender_role: 'user', text: 'newer' },
+ { id: 'r1', sender_role: 'kwant', meta: routed('u-paged-out') },
+ { id: 'u3', sender_role: 'user', thread_id: 't1' },
+ { id: 'r2', sender_role: 'kwant', meta: routed('u3') },
+ ]);
+ assert.equal(q.has('r1'), false);
+ assert.equal(q.has('r2'), false);
+});
+
test('covers / avoids: trimmed, case-insensitive duplicates dropped, at most 20 of 60 characters', () => {
assert.deepEqual(m.addTags(['Taxes'], ' invoices , taxes, VAT returns ').list, ['Taxes', 'invoices', 'VAT returns']);
assert.match(m.addTags([], 'x'.repeat(61)).error, /60 characters/);