Skip to content

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

Keep a /new prompt out of the old reply, and return a failed first turn to the conversation it created.

Shreerama9646196e21bef
diff --git a/src/components/Composer.slash.test.ts b/src/components/Composer.slash.test.ts
index ee326a43ba..4dddbbb1a3 100644
--- a/src/components/Composer.slash.test.ts
+++ b/src/components/Composer.slash.test.ts
@@ -5,7 +5,7 @@ import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Bot, Group } from "@/state/store";
-import { getDraft, getDraftAttachments, restoreComposerDraft } from "@/lib/drafts";
+import { getDraft, getDraftAttachments, noteNewChatLanding, restoreComposerDraft } from "@/lib/drafts";
import { newChatThreadId } from "@/state/new-chat";
const dispatch = vi.hoisted(() => vi.fn());
@@ -273,6 +273,22 @@ describe("the / menu in the composer", () => {
expect(input.value).toBe("");
});
+ it("does not reply to the old conversation, and a failed first turn returns to the conversation that was made", () => {
+ const earlier = { id: "visible-leaf", role: "user" as const, kind: "text" as const, text: "Earlier", at: 1 };
+ const started: Bot = { ...bot, messages: [earlier] };
+ flushSync(() => root.render(createElement(Composer, { bot: started, replyTo: earlier })));
+ const input = fill("/new ship the notes");
+ press(input, "Enter");
+ const sent = dispatch.mock.calls.map(([action]) => action).find((action) => action.type === "send");
+ expect(sent.replyToId).toBeUndefined();
+ expect(sent.threadId).toBe(newChatThreadId(bot.id));
+ restoreComposerDraft(`bot:${bot.id}:${newChatThreadId(bot.id)}`, { text: "", attachments: [] });
+ noteNewChatLanding(sent.sendId, "made-thread");
+ flushSync(() => sent.onError());
+ expect(getDraft(localStorage, `bot:${bot.id}:made-thread`)).toBe("ship the notes");
+ expect(getDraft(localStorage, `bot:${bot.id}:${newChatThreadId(bot.id)}`)).toBe("");
+ });
+
it("clears a bare /new in a room and does not post it", () => {
const room: Group = {
id: "room", threadId: "room-thread", name: "Launch", memberIds: [bot.id],
diff --git a/src/components/Composer.tsx b/src/components/Composer.tsx
index 6007626d9c..458ce19d0c 100644
--- a/src/components/Composer.tsx
+++ b/src/components/Composer.tsx
@@ -605,8 +605,8 @@ export function Composer({
text: freshPrompt || text,
requestText: body,
attachments: [...attachments],
- reply: replyTo ?? null,
- replyToId: replyTo?.id,
+ reply: sentThread === threadId ? replyTo ?? null : null,
+ replyToId: sentThread === threadId ? replyTo?.id : undefined,
threadId: sentThread,
channelMode: effectiveChannelMode, mentions: picks.ids(body), followUp,
};
@@ -628,7 +628,7 @@ export function Composer({
if (freshPrompt && startedThread) dispatch({ type: "newTask", botId: bot.id }); const points = effectiveChannelMode === "goal" ? null : takePointedChips(sentThread);
const page = effectiveChannelMode === "goal" ? undefined : openPageFor(sentThread);
dispatch({
- type: "send", botId: bot.id, text: body, sendId: sentDraft.sendId, replyToId: replyTo?.id, threadId: sentThread, dataContext, mode: effectiveChannelMode, mentions: sentDraft.mentions,
+ type: "send", botId: bot.id, text: body, sendId: sentDraft.sendId, replyToId: sentThread === threadId ? replyTo?.id : undefined, threadId: sentThread, dataContext, mode: effectiveChannelMode, mentions: sentDraft.mentions,
...(points ? { pointed: points.pointed } : {}), ...(page ? { openPage: page } : {}), ...(followUp ? { followUp } : {}),
onError: () => { points?.restore(); restoreDraft(sentDraft); },
});
diff --git a/src/lib/drafts.ts b/src/lib/drafts.ts
index 0da105fbe1..4a608838f6 100644
--- a/src/lib/drafts.ts
+++ b/src/lib/drafts.ts
@@ -421,7 +421,24 @@ export function rememberFailedComposerSend(
return failed;
}
+const newChatLandings = new Map<string, string>();
+
+/** The blank chat's first send is retried in the conversation it created. A failure after that lands in that conversation's draft. */
+export function noteNewChatLanding(sendId: string | undefined, threadId: string): void {
+ if (sendId) newChatLandings.set(sendId, threadId);
+}
+
+function landedSend(sent: ComposerSendSnapshot): ComposerSendSnapshot {
+ const threadId = sent.sendId ? newChatLandings.get(sent.sendId) : undefined;
+ if (!threadId || !sent.threadId?.startsWith("new-chat-") || !sent.draftId.endsWith(`:${sent.threadId}`)) return sent;
+ newChatLandings.delete(sent.sendId);
+ const draftId = `${sent.draftId.slice(0, -sent.threadId.length)}${threadId}`;
+ // The blank chat's revision is not this conversation's. An untouched new conversation takes the prompt back; one the person already edited keeps its text and holds the failure beside it.
+ return { ...sent, threadId, draftId, revision: draftRevision(draftId) === 0 ? 0 : -1, replyToId: undefined };
+}
+
export function recoverFailedComposerSend(sent: ComposerSendSnapshot): "restored" | "outbox" {
+ sent = landedSend(sent);
if (draftRevision(sent.draftId) !== sent.revision) {
rememberFailedComposerSend(sent.draftId, {
sendId: sent.sendId,
diff --git a/src/state/new-chat.ts b/src/state/new-chat.ts
index a038792787..4286e7a51b 100644
--- a/src/state/new-chat.ts
+++ b/src/state/new-chat.ts
@@ -1,4 +1,5 @@
import type { Dispatch } from "react";
+import { noteNewChatLanding } from "@/lib/drafts";
import type { Action, AppState, Bot } from "./store";
/** A blank new chat with one bot: shown in place of its open conversation and made into one by the first send. */
@@ -108,7 +109,10 @@ export function createNewChat(effects: Effects) {
action.onError?.();
return true;
}
- made.then((threadId) => effects.dispatch({ ...action, threadId }), (error: unknown) => {
+ made.then((threadId) => {
+ if (isNewChatThread(action.threadId)) noteNewChatLanding(action.sendId, threadId);
+ effects.dispatch({ ...action, threadId });
+ }, (error: unknown) => {
effects.showError(error);
action.onError?.();
});