Skip to content

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

chathead: keep every thread's context reading in view with Compact now

wayneaa683cd4e442
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsx
index 653c511313..50400f7eed 100644
--- a/src/components/ChatView.tsx
+++ b/src/components/ChatView.tsx
@@ -1085,7 +1085,7 @@ export const ChatView = memo(function ChatView({ bot: profile, inForkPanel = fal
<span className="@max-4xl/chathead:hidden">{t("chat.stop")}</span>
</button>
)}
- <ThreadContextWarning bot={bot} messages={messages} />
+ <ThreadContextWarning bot={bot} />
{phone && <UsageAlert engine={bot.modelSelection?.instanceId} />}
{/* The model and the bot's computer stay in view in both modes; Simple mode moves only ⋯ into the bot panel. */}
{!phone && <ModelPicker key={bot.threadId} bot={bot} threadId={bot.threadId} />}
diff --git a/src/components/ContextWarning.tsx b/src/components/ContextWarning.tsx
index 242e1c0790..c15351f226 100644
--- a/src/components/ContextWarning.tsx
+++ b/src/components/ContextWarning.tsx
@@ -2,11 +2,13 @@ import { useState } from "react";
import { Gauge } from "lucide-react";
import { cn } from "@/lib/cn";
-import { roomContextWarnings, threadContextWarning } from "@/lib/context-warning";
+import { CONTEXT_WARNING_PERCENT, roomContextWarnings, threadContextWarning } from "@/lib/context-warning";
import { t } from "@/lib/i18n";
-import { api, useStore, type Bot, type Group, type Message } from "@/state/store";
+import { formatTokens } from "@/lib/usage";
+import { api, useStore, type Bot, type Group } from "@/state/store";
const pill = "flex items-center gap-1.5 rounded-full border border-warning/40 bg-warning/10 px-2.5 py-1 text-[13px] text-ink";
+const calmPill = "flex items-center gap-1.5 rounded-full border border-hairline/40 bg-raised/50 px-2.5 py-1 text-[13px] text-ink-secondary";
const action = "rounded-full px-1.5 font-medium text-accent hover:underline disabled:cursor-not-allowed disabled:opacity-50 disabled:no-underline";
export function useContextAction() {
@@ -22,14 +24,18 @@ export function useContextAction() {
return { pending, run };
}
-export function ThreadContextWarningView({ name, percent, busy, pending, onCompact }: {
- name: string; percent: number; busy: boolean; pending: boolean; onCompact: () => void;
+export function ThreadContextWarningView({ name, tokens, window, percent, busy, pending, onCompact }: {
+ name: string; tokens: number; window?: number; percent?: number; busy: boolean; pending: boolean; onCompact: () => void;
}) {
+ const danger = percent !== undefined && percent >= CONTEXT_WARNING_PERCENT;
+ const reading = window !== undefined && percent !== undefined
+ ? t("chat.usage.contextOfWindow", { tokens: formatTokens(tokens), percent: String(percent), window: formatTokens(window) })
+ : t("chat.usage.context", { tokens: formatTokens(tokens) });
return (
- <span role="status" data-testid="context-warning" className={pill}
- title={t("contextWarning.threadHint", { name, percent })}>
- <Gauge size={13} className="shrink-0 text-warning" aria-hidden="true" />
- <span className="tabular-nums">{t("contextWarning.short", { percent })}</span>
+ <span role="status" data-testid="context-warning" className={danger ? pill : calmPill}
+ title={percent === undefined ? undefined : t("contextWarning.threadHint", { name, percent })}>
+ <Gauge size={13} className={cn("shrink-0", danger ? "text-warning" : "text-ink-tertiary")} aria-hidden="true" />
+ <span className="tabular-nums">{reading}</span>
<button type="button" className={action} disabled={busy || pending} onClick={onCompact}
title={busy ? t("contextWarning.busy") : undefined}>
{pending ? t("contextWarning.compacting") : t("contextWarning.compactNow")}
@@ -38,13 +44,14 @@ export function ThreadContextWarningView({ name, percent, busy, pending, onCompa
);
}
-export function ThreadContextWarning({ bot, messages }: { bot: Bot; messages: readonly Message[] }) {
+export function ThreadContextWarning({ bot }: { bot: Bot }) {
const usage = bot.tasks?.find((task) => task.threadId === bot.threadId)?.usage;
- const warning = threadContextWarning(usage, messages);
+ const reading = threadContextWarning(usage);
const { pending, run } = useContextAction();
- if (!warning) return null;
+ if (!reading) return null;
return (
- <ThreadContextWarningView name={bot.name} percent={warning.percent} busy={Boolean(bot.busy)} pending={pending}
+ <ThreadContextWarningView name={bot.name} tokens={reading.tokens} window={reading.window} percent={reading.percent}
+ busy={Boolean(bot.busy)} pending={pending}
onCompact={() => run(`/api/bots/${bot.id}/compact`, { threadId: bot.threadId })} />
);
}
diff --git a/src/lib/context-warning.test.ts b/src/lib/context-warning.test.ts
index eeef8d72c5..ccc4eb30bd 100644
--- a/src/lib/context-warning.test.ts
+++ b/src/lib/context-warning.test.ts
@@ -2,27 +2,22 @@ import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it, vi } from "vitest";
-import type { Bot, Message } from "@/state/store";
+import type { Bot } from "@/state/store";
import { RoomContextWarningsView, ThreadContextWarningView } from "@/components/ContextWarning";
-import { CONTEXT_WARNING_PERCENT, compactedSinceReading, roomContextWarnings, threadContextWarning } from "./context-warning";
+import { CONTEXT_WARNING_PERCENT, roomContextWarnings, threadContextWarning } from "./context-warning";
const usage = (tokens: number, window?: number) => ({ input: 0, output: 0, costUsd: null, turns: 1, context: { tokens, ...(window ? { window } : {}) } });
-const line = (role: Message["role"], kind: Message["kind"]) => ({ role, kind });
const bot = (id: string) => ({ id, name: id }) as Bot;
const text = (markup: string) => markup.replace(/<[^>]+>/g, " ").replace(/&#x27;/g, "'").replace(/\s+/g, " ").trim();
describe("context warning", () => {
- it("warns a 1:1 thread from 80% of a known window, and not after a compaction", () => {
+ it("keeps every thread's reading in view, not only a nearly full one", () => {
expect(CONTEXT_WARNING_PERCENT).toBe(80);
- const history = [line("user", "text"), line("bot", "text")];
- expect(threadContextWarning(usage(157_000, 200_000), history)).toBeNull();
- expect(threadContextWarning(usage(180_000, 200_000), history)).toEqual({ percent: 90 });
- expect(threadContextWarning(usage(180_000), history)).toBeNull();
- expect(threadContextWarning(undefined, history)).toBeNull();
- const compacted = [...history, line("bot", "compaction")];
- expect(compactedSinceReading(compacted)).toBe(true);
- expect(threadContextWarning(usage(180_000, 200_000), compacted)).toBeNull();
- expect(compactedSinceReading([...compacted, line("user", "text"), line("bot", "activity"), line("bot", "text")])).toBe(false);
+ expect(threadContextWarning(usage(1_000, 200_000))).toEqual({ tokens: 1_000, window: 200_000, percent: 1 });
+ expect(threadContextWarning(usage(180_000, 200_000))).toEqual({ tokens: 180_000, window: 200_000, percent: 90 });
+ expect(threadContextWarning(usage(180_000))).toEqual({ tokens: 180_000 });
+ expect(threadContextWarning(usage(0, 200_000))).toBeNull();
+ expect(threadContextWarning(undefined)).toBeNull();
});
it("lists room members at 80% or more in roster order, with a pending Compact now", () => {
@@ -41,11 +36,16 @@ describe("context warning", () => {
expect(text(markup)).toBe("Scout's context in this room is 85% full. Compact now Quill's context in this room is 95% full. Compacts before its next turn.");
});
- it("offers Compact now in the 1:1 header, locked while the bot works", () => {
- const idle = renderToStaticMarkup(createElement(ThreadContextWarningView, { name: "Scout", percent: 91, busy: false, pending: false, onCompact: vi.fn() }));
- expect(text(idle)).toBe("Context 91% Compact now");
- expect(idle).not.toContain("disabled=\"\"");
- const busy = renderToStaticMarkup(createElement(ThreadContextWarningView, { name: "Scout", percent: 91, busy: true, pending: false, onCompact: vi.fn() }));
+ it("shows the reading in a quiet pill, turns it into a warning at 80%, and locks Compact now while the bot works", () => {
+ const calm = renderToStaticMarkup(createElement(ThreadContextWarningView, { name: "Scout", tokens: 678_000, window: 1_000_000, percent: 68, busy: false, pending: false, onCompact: vi.fn() }));
+ expect(text(calm)).toBe("Context 678k (68% of 1M) Compact now");
+ expect(calm).not.toContain("bg-warning/10");
+ expect(calm).not.toContain("disabled=\"\"");
+
+ const full = renderToStaticMarkup(createElement(ThreadContextWarningView, { name: "Scout", tokens: 180_000, window: 200_000, percent: 90, busy: false, pending: false, onCompact: vi.fn() }));
+ expect(full).toContain("bg-warning/10");
+
+ const busy = renderToStaticMarkup(createElement(ThreadContextWarningView, { name: "Scout", tokens: 180_000, window: 200_000, percent: 90, busy: true, pending: false, onCompact: vi.fn() }));
expect(busy).toContain("disabled=\"\"");
expect(busy).toContain("Stop this bot&#x27;s turn first");
});
diff --git a/src/lib/context-warning.ts b/src/lib/context-warning.ts
index 8b1373da19..accf646a9d 100644
--- a/src/lib/context-warning.ts
+++ b/src/lib/context-warning.ts
@@ -1,20 +1,23 @@
-import type { Bot, Group, Message, TaskUsage } from "@/state/store";
+import type { Bot, Group, TaskUsage } from "@/state/store";
import { contextShare } from "./usage";
export const CONTEXT_WARNING_PERCENT = 80;
-export function compactedSinceReading(messages: readonly Pick<Message, "role" | "kind">[]): boolean {
- const compaction = messages.findLastIndex((message) => message.kind === "compaction");
- if (compaction < 0) return false;
- const reply = messages.findLastIndex((message) => message.role === "bot" && message.kind === "text");
- return compaction > reply;
+/** The header pill's reading: every thread whose last settled turn filled part of a known window keeps it in view. */
+export interface ThreadContextReading {
+ tokens: number;
+ window?: number;
+ percent?: number;
}
-export function threadContextWarning(usage: TaskUsage | undefined, messages: readonly Pick<Message, "role" | "kind">[]): { percent: number } | null {
+export function threadContextWarning(usage: TaskUsage | undefined): ThreadContextReading | null {
const share = usage ? contextShare(usage) : null;
- if (share?.percent === undefined || share.percent < CONTEXT_WARNING_PERCENT) return null;
- if (compactedSinceReading(messages)) return null;
- return { percent: share.percent };
+ if (!share) return null;
+ return {
+ tokens: share.tokens,
+ ...(share.window !== undefined ? { window: share.window } : {}),
+ ...(share.percent !== undefined ? { percent: share.percent } : {}),
+ };
}
export function roomContextWarnings(group: Pick<Group, "memberContext">, members: readonly Bot[]):