mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 05:57:09 +01:00
feat(loki): suggested replies on every Loki chat — one tap sends the likely next message (#1115)
Adopts @bitbaum/chatkit 0.6.0 (bitbaum/chatkit#13, merged as 163c879). - askLoki/runLokiTurn take `replies`: the model is asked for chatkit's quick_replies block, and it is taken out of every model reply as it arrives (loop rounds, retries, gateway and Groq fallbacks), so it never reaches a work note, the grounding verifier, or the stored answer. Replies return separately as body.replies. - /loki: the messages route asks for replies and stores them in meta.replies; the thread shows them under the latest answer with chatkit's ChatReplies (the separate follow-ups call is now only the fallback for an answer that came without them). - Terminal Loki rail: ChatThread gets onReply; replies hidden under a summary that already carries its own "→" steps. - Floating assistant: /api/loki takes `replies`; buttons under the last answer. - The live preview is stripped once, in useLokiStream, for every surface; plainTextForSpeech drops the block so it is never read aloud. MCP, voice and prompt runs do not ask for replies, and a stray block is still removed from their text. Fixes found on the way: - Floating assistant printed answers as raw text (asterisks, bare [F1] handles): now rendered as markdown with resolved citations; multi-line user turns keep their line breaks. - /loki follow-up taps were sent on "Auto" instead of the model the operator last chose; the terminal rail's replies reuse its last choice. Claude-Session: https://claude.ai/code/session_01Mg5wrkfw95JWfmAz8yRT1j Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
07b1fdee80
commit
e52c65af80
15 files changed
+410
-107
No files matched your search
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* Suggested replies (chatkit's `quick_replies` block) on every Loki chat.
|
||||
* Run: npx tsx scripts/test/loki-suggested-replies.ts
|
||||
*
|
||||
* The model writes the replies at the end of its answer, so the one thing that
|
||||
* must never happen is the block travelling WITH the answer: into the stored
|
||||
* message (and from there into copy, save-to-memory, titles, previews and the
|
||||
* next turn's history), into a work-trail note, or into speech. Env-independent:
|
||||
* the model call and the seed are injected.
|
||||
*/
|
||||
import assert from "node:assert/strict";
|
||||
import { REPLIES_INSTRUCTION } from "@bitbaum/chatkit";
|
||||
import type { ModelTurn } from "../../src/lib/agent/llm";
|
||||
import type { ToolRegistry } from "../../src/lib/agent/tools/registry";
|
||||
import { runLokiTurn, type LokiTurnEvent } from "../../src/lib/agent/loop";
|
||||
import { readReplies } from "../../src/lib/loki/replies";
|
||||
import { plainTextForSpeech } from "../../src/lib/loki/speech-text";
|
||||
|
||||
const BLOCK = '\n\n```quick_replies\n["Yes, run it", "Show me the diff first", "Not now"]\n```';
|
||||
const SEED = { facts: [], directives: [] };
|
||||
const NO_TOOLS: ToolRegistry = {};
|
||||
|
||||
/** One scripted answer; records the system prompt each call was given. */
|
||||
function model(text: string) {
|
||||
const systems: string[] = [];
|
||||
const fn = async (input: { messages: Array<{ role: string; content: string }> }) => {
|
||||
systems.push(input.messages.find((m) => m.role === "system")?.content ?? "");
|
||||
const turn: ModelTurn = { text, toolCalls: [], model: "stub", cutOff: null, usageTokens: 0 };
|
||||
return turn;
|
||||
};
|
||||
return { fn: fn as never, systems };
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// ── A chat turn asks for replies and gets them back APART from the text ────
|
||||
{
|
||||
const m = model(`The deploy is green.${BLOCK}`);
|
||||
const r = await runLokiTurn({
|
||||
userId: "u1",
|
||||
message: "is it deployed?",
|
||||
registry: NO_TOOLS,
|
||||
seed: SEED,
|
||||
callModel: m.fn,
|
||||
replies: true,
|
||||
});
|
||||
assert.ok(m.systems[0].includes(REPLIES_INSTRUCTION), "a chat turn asks for replies");
|
||||
assert.equal(r.text, "The deploy is green.", "the block never travels with the answer");
|
||||
assert.deepEqual(r.replies, ["Yes, run it", "Show me the diff first", "Not now"]);
|
||||
}
|
||||
|
||||
// ── Off (MCP, voice, prompt runs): not asked for, and stripped if sent anyway
|
||||
{
|
||||
const m = model(`The deploy is green.${BLOCK}`);
|
||||
const r = await runLokiTurn({
|
||||
userId: "u1",
|
||||
message: "is it deployed?",
|
||||
registry: NO_TOOLS,
|
||||
seed: SEED,
|
||||
callModel: m.fn,
|
||||
});
|
||||
assert.ok(!m.systems[0].includes("quick_replies"), "a non-chat caller is not asked");
|
||||
assert.equal(r.text, "The deploy is green.", "an unasked-for block is still removed");
|
||||
assert.deepEqual(r.replies, [], "and its replies are not handed to a caller with no buttons");
|
||||
}
|
||||
|
||||
// ── A gathering round's note never carries the block ───────────────────────
|
||||
{
|
||||
let call = 0;
|
||||
const fn = async (): Promise<ModelTurn> => {
|
||||
call++;
|
||||
return call === 1
|
||||
? {
|
||||
text: `Looking at the runs first.${BLOCK}`,
|
||||
toolCalls: [{ id: "1", name: "probe", args: {} }],
|
||||
model: "stub",
|
||||
cutOff: null,
|
||||
usageTokens: 0,
|
||||
}
|
||||
: { text: "All runs passed.", toolCalls: [], model: "stub", cutOff: null, usageTokens: 0 };
|
||||
};
|
||||
const { z } = await import("zod");
|
||||
const { defineTool } = await import("../../src/lib/agent/tools/registry");
|
||||
const registry: ToolRegistry = {
|
||||
probe: defineTool({
|
||||
name: "probe",
|
||||
kind: "read",
|
||||
description: "stub",
|
||||
params: z.object({}),
|
||||
example: "TOOL: probe\nARGS: {}",
|
||||
handler: async () => ({ facts: [], note: "nothing new" }),
|
||||
}),
|
||||
};
|
||||
const events: LokiTurnEvent[] = [];
|
||||
const r = await runLokiTurn({
|
||||
userId: "u1",
|
||||
message: "how are the runs?",
|
||||
registry,
|
||||
seed: SEED,
|
||||
callModel: fn as never,
|
||||
replies: true,
|
||||
onEvent: (e) => events.push(e),
|
||||
});
|
||||
const notes = r.work.filter((w) => w.kind === "note");
|
||||
assert.equal(notes.length, 1);
|
||||
assert.doesNotMatch(JSON.stringify(notes), /quick_replies/, "a stored note has no block");
|
||||
assert.doesNotMatch(
|
||||
JSON.stringify(events.filter((e) => e.type === "note")),
|
||||
/quick_replies/,
|
||||
"a streamed note has no block",
|
||||
);
|
||||
}
|
||||
|
||||
// ── Stored replies read back defensively ───────────────────────────────────
|
||||
assert.deepEqual(readReplies({ replies: ["Yes", 3, "", "No"] }), ["Yes", "No"]);
|
||||
assert.deepEqual(readReplies(null), []);
|
||||
assert.deepEqual(readReplies({ replies: "Yes" }), [], "only an array is replies");
|
||||
|
||||
// ── Never spoken aloud ──────────────────────────────────────────────────────
|
||||
const spoken = plainTextForSpeech(`The deploy is green.${BLOCK}`);
|
||||
assert.equal(spoken, "The deploy is green.", "the block is neither read nor named as code");
|
||||
|
||||
console.log("loki-suggested-replies: ok");
|
||||
}
|
||||
|
||||
main().catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user