Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Train rework for #6109: the header pill keeps its 80% gate
milind-soni49159407c732
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsxindex 58a130004a..45e4375062 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} />+ <ThreadContextWarning bot={bot} messages={messages} />{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.tsxindex f44aabb305..bba888c9e1 100644--- a/src/components/ContextWarning.tsx+++ b/src/components/ContextWarning.tsx@@ -5,7 +5,7 @@ import { cn } from "@/lib/cn";import { CONTEXT_WARNING_PERCENT, roomContextWarnings, threadContextWarning } from "@/lib/context-warning";import { t } from "@/lib/i18n";import { formatTokens } from "@/lib/usage";-import { api, useStore, type Bot, type Group } from "@/state/store";+import { api, useStore, type Bot, type Group, type Message } 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";@@ -44,9 +44,9 @@ export function ThreadContextWarningView({ name, tokens, window, percent, busy,);}-export function ThreadContextWarning({ bot }: { bot: Bot }) {+export function ThreadContextWarning({ bot, messages }: { bot: Bot; messages: readonly Message[] }) {const usage = bot.tasks?.find((task) => task.threadId === bot.threadId)?.usage;- const reading = threadContextWarning(usage);+ const reading = threadContextWarning(usage, messages);const { pending, run } = useContextAction();if (!reading) return null;return (diff --git a/src/lib/context-warning.test.ts b/src/lib/context-warning.test.tsindex bd050fc2ac..6c5489d785 100644--- a/src/lib/context-warning.test.ts+++ b/src/lib/context-warning.test.ts@@ -2,22 +2,27 @@ import { createElement } from "react";import { renderToStaticMarkup } from "react-dom/server";import { describe, expect, it, vi } from "vitest";-import type { Bot } from "@/state/store";+import type { Bot, Message } from "@/state/store";import { RoomContextWarningsView, ThreadContextWarningView } from "@/components/ContextWarning";-import { CONTEXT_WARNING_PERCENT, roomContextWarnings, threadContextWarning } from "./context-warning";+import { CONTEXT_WARNING_PERCENT, compactedSinceReading, 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(/'/g, "'").replace(/\s+/g, " ").trim();describe("context warning", () => {- it("keeps every thread's reading in view, not only a nearly full one", () => {+ it("warns a 1:1 thread from 80% of a known window with its tokens, and not after a compaction", () => {expect(CONTEXT_WARNING_PERCENT).toBe(80);- 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();+ 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({ tokens: 180_000, window: 200_000, 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);});it("lists room members at 80% or more in roster order, with a pending Compact now", () => {diff --git a/src/lib/context-warning.ts b/src/lib/context-warning.tsindex f74a41fddf..18fd154707 100644--- a/src/lib/context-warning.ts+++ b/src/lib/context-warning.ts@@ -1,23 +1,28 @@-import type { Bot, Group, TaskUsage } from "@/state/store";+import type { Bot, Group, Message, TaskUsage } from "@/state/store";import { contextShare } from "./usage";export const CONTEXT_WARNING_PERCENT = 80;-/** The header pill's reading: every thread whose last settled turn filled part of a known window keeps it in view. */+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, from 80% of a known window until the conversation is compacted. The message box's+ * context button keeps a reading for every thread, so the header stays quiet below that. */export interface ThreadContextReading {tokens: number;- window?: number;- percent?: number;+ window: number;+ percent: number;}-export function threadContextWarning(usage: TaskUsage | undefined): ThreadContextReading | null {+export function threadContextWarning(usage: TaskUsage | undefined, messages: readonly Pick<Message, "role" | "kind">[]): ThreadContextReading | null {const share = usage ? contextShare(usage) : null;- if (!share) return null;- return {- tokens: share.tokens,- ...(share.window !== undefined ? { window: share.window } : {}),- ...(share.percent !== undefined ? { percent: share.percent } : {}),- };+ if (!share || share.window === undefined || share.percent === undefined || share.percent < CONTEXT_WARNING_PERCENT) return null;+ if (compactedSinceReading(messages)) return null;+ return { tokens: share.tokens, window: share.window, percent: share.percent };}export function roomContextWarnings(group: Pick<Group, "memberContext">, members: readonly Bot[]):