Skip to content

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

Keep an open key or connect card on screen in a long chat

AdityaUmalef378a364e523
diff --git a/shared/person-wait.test.ts b/shared/person-wait.test.ts
index 4feeeefe33..7ee231d188 100644
--- a/shared/person-wait.test.ts
+++ b/shared/person-wait.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { openPersonWait, personWait, restingOnPerson } from "./person-wait.ts";
+import { openPersonWait, personWait, restingOnPerson, staysInView } from "./person-wait.ts";
import type { WireMessage } from "./wire.ts";
const row = (fields: Partial<WireMessage>): WireMessage => ({ id: "m", role: "bot", kind: "text", at: 5, ...fields }) as WireMessage;
@@ -64,3 +64,15 @@ describe("a conversation's visible branch", () => {
expect(restingOnPerson([approval])).toBeUndefined();
});
});
+
+describe("cards that stay in view", () => {
+ it("are an open key or app to connect, not a question, sign-in, or a settled or first-hour card", () => {
+ expect(staysInView(secret())).toBe(true);
+ expect(staysInView(connector())).toBe(true);
+ expect(staysInView(secret({ dismissed: true }))).toBe(false);
+ expect(staysInView(connector({ resumeKey: "first-hour-gmail" }))).toBe(false);
+ expect(staysInView(signIn())).toBe(false);
+ expect(staysInView(options({ subtitle: "Which day?" }))).toBe(false);
+ expect(staysInView(row({}))).toBe(false);
+ });
+});
diff --git a/shared/person-wait.ts b/shared/person-wait.ts
index 73ecc80c3a..5666a5f662 100644
--- a/shared/person-wait.ts
+++ b/shared/person-wait.ts
@@ -65,3 +65,9 @@ export function restingOnPerson(path: readonly WaitRow[]): PersonWait | undefine
}
return undefined;
}
+
+/** A key to enter or an app to connect, still open: the cards that stay in view until the person answers them. */
+export function staysInView(row: WaitRow): boolean {
+ const kind = personWait(row)?.kind;
+ return kind === "password" || kind === "connect";
+}
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsx
index 2b4a032f18..e08a47d197 100644
--- a/src/components/ChatView.tsx
+++ b/src/components/ChatView.tsx
@@ -118,7 +118,7 @@ import { splitTranscriptAttachments } from "@/lib/composer-attachments";
import { useComposerDockPad } from "@/lib/composer-dock";
import { GlassBar, GlassScrollFrame } from "./GlassScrollFrame";
import { PagePaneContext } from "./app/PageTools";
-import { useTranscriptViewport } from "@/hooks/use-transcript-viewport";
+import { useTranscriptViewport } from "@/hooks/use-transcript-viewport"; import { staysInView } from "../../shared/person-wait";
import { useUnreadDivider } from "@/hooks/use-unread-divider";
import { NewMessagesDivider } from "./NewMessagesDivider";
import { unreadMessageIds } from "@/lib/unread-divider";
@@ -856,7 +856,7 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = fal
threadId: bot.threadId,
messages,
pinOn: [bot.busy, composerDock.pad],
- hasMore: bot.hasMore,
+ hasMore: bot.hasMore, rests: staysInView,
});
const lastBotTextId = useMemo(
diff --git a/src/components/GroupView.tsx b/src/components/GroupView.tsx
index b17747b302..56e6af5020 100644
--- a/src/components/GroupView.tsx
+++ b/src/components/GroupView.tsx
@@ -81,7 +81,7 @@ import { GlassBar, GlassScrollFrame } from "./GlassScrollFrame";
import { awaitedMemberId, showWorkingDots } from "@/lib/turn-tail";
import { turnNowPhrases } from "@/lib/live-activity"; import { useTurnLive } from "@/state/turn-status"; import { isLiveReply, StreamingReplyContext, streamedReply, useLiveTranscript } from "@/state/live-reply";
import { splitTranscriptAttachments } from "@/lib/composer-attachments";
-import { useTranscriptViewport } from "@/hooks/use-transcript-viewport";
+import { useTranscriptViewport } from "@/hooks/use-transcript-viewport"; import { staysInView } from "../../shared/person-wait";
import { useUnreadDivider } from "@/hooks/use-unread-divider";
import { unreadMessageIds } from "@/lib/unread-divider";
import { NewMessagesDivider } from "./NewMessagesDivider";
@@ -986,7 +986,7 @@ export function GroupView({ group, inForkPanel = false }: { group: Group; inFork
messages: group.messages,
pinOn: [group.busyBotId, group.working, composerDock.pad],
transcriptShown: !setupPending,
- hasMore: group.hasMore,
+ hasMore: group.hasMore, rests: staysInView,
});
const unreadDivider = useUnreadDivider({ threadId: group.threadId, messages: group.messages, following });
diff --git a/src/hooks/use-transcript-viewport.test.ts b/src/hooks/use-transcript-viewport.test.ts
index 196e9afb5b..396edc6a14 100644
--- a/src/hooks/use-transcript-viewport.test.ts
+++ b/src/hooks/use-transcript-viewport.test.ts
@@ -268,6 +268,18 @@ describe("transcript viewport", () => {
expect(view.current.windowedMessages[0]?.id).toBe("m1180");
});
+ it("keeps the open card work rests on drawn however many rows follow it", () => {
+ const rests = (row: Row) => row.id === "card";
+ const thread = [...rows(5), { id: "card" }, ...rows(200, 5)];
+ const view = mount({ messages: thread, rests });
+ expect(view.current.hiddenCount).toBe(thread.length - 120);
+ expect(view.current.windowedMessages[0]?.id).toBe("card");
+ expect(view.current.windowedMessages.slice(1)).toEqual(thread.slice(-120));
+
+ view.rerender({ messages: thread, rests: () => false });
+ expect(view.current.windowedMessages).toEqual(thread.slice(-120));
+ });
+
it("keeps the window bounded while the reader follows new rows", () => {
const view = mount({ messages: rows(300) });
for (let total = 301; total <= 900; total++) {
diff --git a/src/hooks/use-transcript-viewport.ts b/src/hooks/use-transcript-viewport.ts
index d3fb70dc0a..ac7068c55a 100644
--- a/src/hooks/use-transcript-viewport.ts
+++ b/src/hooks/use-transcript-viewport.ts
@@ -19,6 +19,7 @@ import {
followWindowStart,
resolveTranscriptWindow,
tailWindowStart,
+ withRestingCard,
} from "@/lib/transcript-window";
import { reducedMotion } from "@/lib/onboarding";
import {
@@ -65,6 +66,7 @@ export function useTranscriptViewport<T extends { id: string; role?: string }>({
pinOn,
transcriptShown = true,
hasMore = false,
+ rests,
}: {
ownerId: string;
threadId: string;
@@ -72,6 +74,8 @@ export function useTranscriptViewport<T extends { id: string; role?: string }>({
pinOn: DependencyList;
transcriptShown?: boolean;
hasMore?: boolean;
+ /** A card work rests on until the person answers it; the newest one stays drawn when the window has left it behind. */
+ rests?: (row: T) => boolean;
}) {
const focusMessage = useStoreSelector((state) => state.focusMessage);
const olderPending = useStoreSelector((state) => Boolean(state.loadingOlder[threadId]));
@@ -113,10 +117,10 @@ export function useTranscriptViewport<T extends { id: string; role?: string }>({
laterCount,
startIndex,
endIndex,
- } = useMemo(
- () => resolveTranscriptWindow(messages, transcriptWindow.start, TRANSCRIPT_WINDOW_SIZE, transcriptWindow.end),
- [messages, transcriptWindow.start, transcriptWindow.end],
- );
+ } = useMemo(() => {
+ const range = resolveTranscriptWindow(messages, transcriptWindow.start, TRANSCRIPT_WINDOW_SIZE, transcriptWindow.end);
+ return { ...range, visible: withRestingCard(messages, range.visible, range.startIndex, rests) };
+ }, [messages, transcriptWindow.start, transcriptWindow.end, rests]);
const windowRef = useRef(transcriptWindow);
windowRef.current = transcriptWindow;
diff --git a/src/lib/transcript-window.test.ts b/src/lib/transcript-window.test.ts
index afd6987514..97e52701eb 100644
--- a/src/lib/transcript-window.test.ts
+++ b/src/lib/transcript-window.test.ts
@@ -7,6 +7,7 @@ import {
followWindowStart,
resolveTranscriptWindow,
tailWindowStart,
+ withRestingCard,
} from "./transcript-window";
const thread = (total: number): number[] => Array.from({ length: total }, (_, i) => i);
@@ -166,3 +167,23 @@ describe("focusWindowRange", () => {
expect(focusWindowRange(20, 10)).toEqual({ start: 0, end: 20 });
});
});
+
+describe("withRestingCard", () => {
+ const rows = thread(300);
+ const range = resolveTranscriptWindow(rows, tailWindowStart(rows.length));
+
+ it("puts the newest resting card back on top when the range has left it behind", () => {
+ const shown = withRestingCard(rows, range.visible, range.startIndex, (row) => row === 10 || row === 40);
+ expect(shown[0]).toBe(40);
+ expect(shown.slice(1)).toEqual(range.visible);
+ });
+
+ it("adds nothing when the newest resting card is already in the range", () => {
+ expect(withRestingCard(rows, range.visible, range.startIndex, (row) => row === 10 || row === 250)).toBe(range.visible);
+ });
+
+ it("adds nothing when no card rests on the person", () => {
+ expect(withRestingCard(rows, range.visible, range.startIndex, () => false)).toBe(range.visible);
+ expect(withRestingCard(rows, range.visible, range.startIndex)).toBe(range.visible);
+ });
+});
diff --git a/src/lib/transcript-window.ts b/src/lib/transcript-window.ts
index b3a236143c..b5e6af940f 100644
--- a/src/lib/transcript-window.ts
+++ b/src/lib/transcript-window.ts
@@ -63,3 +63,10 @@ export function resolveTranscriptWindow<T>(
endIndex: end,
};
}
+
+/** The window's rows, with the newest card that `rests` on put back on top when the window has left it behind: work rests
+ * on that card until the person answers it, so its answer and its Not now stay on screen however long the chat grows. */
+export function withRestingCard<T>(messages: readonly T[], visible: T[], startIndex: number, rests?: (row: T) => boolean): T[] {
+ const index = rests ? messages.findLastIndex(rests) : -1;
+ return index >= 0 && index < startIndex ? [messages[index]!, ...visible] : visible;
+}