Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

A line typed in a bot's own chat while its question card waits answers the card, or closes one that takes no words

luishron123daac62594e3f
diff --git a/server/host/ask-cards.test.ts b/server/host/ask-cards.test.ts
index 84ee96d96c..2c3e67f367 100644
--- a/server/host/ask-cards.test.ts
+++ b/server/host/ask-cards.test.ts
@@ -42,7 +42,7 @@ function world(options: { inboxThread?: string; live?: string[]; privateThreads?
const nodes = new Map<string, HandoffView>();
const answered: Array<{ id: string; text: string }> = [];
const notified: Notification[] = [];
- const answers: Array<{ threadId: string; requestId: string; message: string }> = [];
+ const answers: Array<{ threadId: string; requestId: string; message?: string; behavior?: "deny" }> = [];
let refuse: { status: number; error: string } | null = null;
const deps: AskCardsDeps = {
store: store as unknown as AskCardsDeps["store"],
@@ -54,7 +54,7 @@ function world(options: { inboxThread?: string; live?: string[]; privateThreads?
live: (_threadId, requestId) => (options.live ?? []).includes(requestId),
answerCard: vi.fn(async (input) => {
if (refuse) return { ok: false as const, ...refuse };
- answers.push({ threadId: input.threadId, requestId: input.requestId, message: input.message });
+ answers.push({ threadId: input.threadId, requestId: input.requestId, ...(input.behavior === "deny" ? { behavior: "deny" as const } : { message: input.message }) });
return { ok: true as const };
}),
answeredIn: (threadId, requestId) => (threadId === "inbox" && requestId.startsWith("req") ? "work" : threadId),
@@ -346,12 +346,38 @@ describe("a typed line answers a question card", () => {
expect(fixed.answers).toEqual([]);
});
- it("leaves a line in a chat no teammate handed on to be steered or queued, even while its question card waits", async () => {
+ it("answers the card its turn waits on in the bot's own chat, telling the bot the words were typed, not picked", async () => {
const w = world({ live: ["req-1"] });
const asked = question(w, "chat");
- expect(await w.cards.typed(AUTH, "chat", "Take the second one", undefined, {})).toBeNull();
- expect(await w.cards.typed(AUTH, "chat", "Take the second one", asked, {})).toBeNull();
- expect(w.answers).toEqual([]);
+ const result = await w.cards.typed(AUTH, "chat", "What time is it?", undefined, { sendId: "send-3" });
+ expect(w.answers).toEqual([{ threadId: "chat", requestId: "req-1",
+ message: "[The person typed this in the chat instead of picking an option. Reply to it, and ask again if you still need an answer.] What time is it?" }]);
+ expect(w.rows("chat").find((row) => row.id === asked.id)?.card?.answeredText).toBe("What time is it?");
+ expect(result).toMatchObject({ status: 202, message: { role: "user", text: "What time is it?", replyToId: asked.id, sendId: "send-3" } });
+ });
+
+ it("closes a card that takes no words in the bot's own chat and leaves the line to be sent as the next message", async () => {
+ for (const card of [{ fixedOptions: true }, { questionRequest: { version: 1 as const, questions: [{ question: "A?", options: [{ label: "x" }] }, { question: "B?", options: [{ label: "y" }] }] } }]) {
+ const w = world({ live: ["req-1"] });
+ question(w, "chat", card);
+ expect(await w.cards.typed(AUTH, "chat", "Something else first", undefined, {})).toBeNull();
+ expect(w.answers).toEqual([{ threadId: "chat", requestId: "req-1", behavior: "deny" }]);
+ expect(w.rows("chat").filter((row) => row.role === "user")).toEqual([]);
+ }
+ });
+
+ it("leaves a line in the bot's own chat to the usual send when no turn waits on its card, it is Stop, it replies to another line or carries a file", async () => {
+ const idle = world();
+ question(idle, "chat");
+ expect(await idle.cards.typed(AUTH, "chat", "Take the second one", undefined, {})).toBeNull();
+ const busy = world({ live: ["req-1"] });
+ question(busy, "chat");
+ const other = busy.say("chat", "I checked.");
+ expect(await busy.cards.typed(AUTH, "chat", "stop", undefined, {})).toBeNull();
+ expect(await busy.cards.typed(AUTH, "chat", "Thanks", other, {})).toBeNull();
+ expect(await busy.cards.typed(AUTH, "chat", 'Look <attached-image path="a.png" />', undefined, {})).toBeNull();
+ expect(idle.answers).toEqual([]);
+ expect(busy.answers).toEqual([]);
});
it("passes on why an answer was refused, and writes no line", async () => {
diff --git a/server/host/ask-cards.ts b/server/host/ask-cards.ts
index 821e3d3cee..0831c62be8 100644
--- a/server/host/ask-cards.ts
+++ b/server/host/ask-cards.ts
@@ -24,6 +24,8 @@ import { settledWaitingChip } from "../waiting-on-person.ts";
const OPEN_QUESTION = "needs-input-";
const DAY_MS = 24 * 60 * 60_000;
const NEEDS_INPUT_LINE = /^needs input:/i;
+const TYPED_IN_CHAT = "[The person typed this in the chat instead of picking an option. Reply to it, and ask again if you still need an answer.]";
+const hasAttachment = (text: string): boolean => text.includes("<attached-image") || text.includes("<attached-file");
export interface AskCardsDeps {
store: Pick<Store, "bot" | "messagesFor" | "activePath" | "appendMessage" | "patchMessage" | "taskByThread" | "patchTask" | "group"
@@ -41,7 +43,7 @@ export interface AskCardsDeps {
/** The parked node's next turn carries the person's answer. */
answerNode(id: string, text: string): boolean;
/** Answers a card as a tap on it would, with the same checks on who may answer, through an inbox copy too. */
- answerCard(input: { auth: RequestAuth; threadId: string; requestId: string; behavior: "answer"; message: string }): Promise<{ ok: true } | { ok: false; status: number; error: string }>;
+ answerCard(input: { auth: RequestAuth; threadId: string; requestId: string; behavior: "answer" | "deny"; message?: string }): Promise<{ ok: true } | { ok: false; status: number; error: string }>;
/** The conversation a card in `threadId` was asked in: an inbox copy's original. */
answeredIn(threadId: string, requestId: string): string;
now?: () => number;
@@ -299,24 +301,36 @@ export function createAskCards(deps: AskCardsDeps) {
return true;
}
- /** A person's line answers an inbox copy it replies to, or, in work a teammate handed on, the question card the bot's
- * turn waits on, as a tap would; null leaves the line to be sent (steered or queued) as usual. */
+ /** A person's line answers an inbox copy it replies to, or the question card the bot's turn waits on, as a tap would.
+ * In the bot's own chat a card that takes no words closes instead, and the line goes on as the next message. Null
+ * leaves the line to be sent (steered or queued) as usual. */
async function typed(auth: RequestAuth, threadId: string, text: string, target: Message | undefined,
line: { sendId?: string; sender?: Message["sender"] }): Promise<{ status: number; message?: Message; error?: string } | null> {
if (store.groupByThread(threadId) || isStopCommand(text)) return null;
const delegator = delegatorOf(threadId);
const handedOn = Boolean(delegator && delegator.threadId !== threadId);
+ const waiting = store.messagesFor(threadId).findLast((message) => !message.inbox && liveCardKind(message.card) === "question"
+ && deps.live(threadId, message.card!.requestId!));
+ const ownChat = !handedOn && !hasAttachment(text) && Boolean(waiting) && (!target || target.id === waiting!.id);
+ if (ownChat && !answerableInWords(waiting!.card)) {
+ await deps.answerCard({ auth, threadId, requestId: waiting!.card!.requestId!, behavior: "deny" });
+ return null;
+ }
const asked = target
? answerableInWords(target.card) ? target : undefined
- : handedOn ? store.messagesFor(threadId).findLast((message) => answerableInWords(message.card) && deps.live(threadId, message.card!.requestId!)) : undefined;
+ : ownChat ? waiting
+ : handedOn ? store.messagesFor(threadId).findLast((message) => answerableInWords(message.card) && deps.live(threadId, message.card!.requestId!)) : undefined;
if (!asked?.card?.requestId) return null;
const requestId = asked.card.requestId;
const origin = deps.answeredIn(threadId, requestId);
- if (origin === threadId && !(handedOn && deps.live(threadId, requestId))) return null;
+ if (origin === threadId && !((handedOn || ownChat) && deps.live(threadId, requestId))) return null;
const again = line.sendId ? store.messagesFor(threadId).find((message) => message.sendId === line.sendId) : undefined;
if (again) return { status: 202, message: again };
- const answered = await deps.answerCard({ auth, threadId, requestId, behavior: "answer", message: typedAnswer(asked.card, text) });
+ const inChat = ownChat && origin === threadId;
+ const answered = await deps.answerCard({ auth, threadId, requestId, behavior: "answer", message: typedAnswer(asked.card, inChat ? `${TYPED_IN_CHAT} ${text}` : text) });
if (!answered.ok) return { status: answered.status, error: answered.error };
+ const settled = inChat ? store.messagesFor(threadId).find((message) => message.id === asked.id)?.card : undefined;
+ if (settled) store.patchMessage(threadId, asked.id, { card: { ...settled, answeredText: text } });
const posted = store.appendMessage(threadId, { role: "user", kind: "text", text, replyToId: asked.id,
...(line.sendId ? { sendId: line.sendId } : {}), ...(line.sender ? { sender: line.sender } : {}) });
return { status: 202, message: posted };