Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
OpenAI-compatible: ask llama.cpp again with its fresh count when a smaller copy is still over
Absterrg02c8380382072
diff --git a/server/drivers/chat-size-limit.test.ts b/server/drivers/chat-size-limit.test.tsindex b6220ab038..5adbf27cf9 100644--- a/server/drivers/chat-size-limit.test.ts+++ b/server/drivers/chat-size-limit.test.ts@@ -140,6 +140,24 @@ describe("smallerResend", () => {expect(counted.after.messages).toBeGreaterThan(halved.after.messages);});+ it("counts what an earlier omission note stood for when a second copy drops more", () => {+ const { request, messages, refused } = turn(history(10, 4_000));+ const first = smallerResend(messages, request, refused, 0.8)!;+ const second = smallerResend(first.messages, request, first.after.bytes, 0.6)!;+ const notes = second.messages.filter((message) => typeof message.content === "string" && message.content.includes("omitted to fit"));+ const earlier = second.messages.indexOf(request) - 2;+ expect(notes).toEqual([{ role: "assistant", content: `[${10 - earlier} earlier messages omitted to fit the provider's size limit; full history remains in the app.]` }]);+ expect(second.messages[1]).toBe(notes[0]);+ expect(10 - earlier).toBeGreaterThan(10 - (first.messages.indexOf(request) - 2));+ });++ it("offers nothing to resend when only an omission note stands before the request", () => {+ const { request, messages, refused } = turn(history(4, 4_000));+ const first = smallerResend(messages, request, refused, 0.01)!;+ expect(first.messages).toHaveLength(3);+ expect(smallerResend(first.messages, request, first.after.bytes, 0.01)).toBeNull();+ });+it("offers nothing to resend when only the system prompt and the request are left", () => {const { request, messages, refused } = turn([]);expect(smallerResend(messages, request, refused)).toBeNull();@@ -153,7 +171,7 @@ describe("an OpenAI-compatible provider that refuses large requests", () => {vi.restoreAllMocks();});- async function provider(refuse: (bytes: number) => { status: number; body: string } | null) {+ async function provider(refuse: (bytes: number, messages: Array<{ role: string; content: unknown }>) => { status: number; body: string } | null) {const requests: Array<{ bytes: number; messages: Array<{ role: string; content: unknown }> }> = [];const server = createServer(async (req, res) => {if (req.url === "/v1/models") {@@ -166,8 +184,9 @@ describe("an OpenAI-compatible provider that refuses large requests", () => {if (req.method !== "POST") { res.writeHead(404); res.end(); return; }for await (const chunk of req) chunks.push(chunk as Buffer);const raw = Buffer.concat(chunks);- requests.push({ bytes: raw.length, messages: JSON.parse(raw.toString("utf8")).messages });- const refusal = refuse(raw.length);+ const { messages } = JSON.parse(raw.toString("utf8")) as { messages: Array<{ role: string; content: unknown }> };+ requests.push({ bytes: raw.length, messages });+ const refusal = refuse(raw.length, messages);if (refusal) {res.writeHead(refusal.status, { "content-type": "text/html" });res.end(refusal.body);@@ -240,15 +259,18 @@ describe("an OpenAI-compatible provider that refuses large requests", () => {});const tokens = (bytes: number) => Math.ceil(bytes / 4);- const llamaCpp = (window: (firstTokens: number) => number) => {+ const llamaCpp = (window: (firstTokens: number) => number, count: (bytes: number, messages: Array<{ role: string; content: unknown }>) => number = tokens) => {let n_ctx: number | undefined;- return (bytes: number) => {- n_ctx ??= window(tokens(bytes));- return tokens(bytes) > n_ctx ? { status: 400, body: JSON.stringify({ error: { code: 400,- message: `request (${tokens(bytes)} tokens) exceeds the available context size (${n_ctx} tokens), try increasing it`,- type: "exceed_context_size_error", n_prompt_tokens: tokens(bytes), n_ctx } }) } : null;+ return (bytes: number, messages: Array<{ role: string; content: unknown }>) => {+ const n_prompt_tokens = count(bytes, messages);+ n_ctx ??= window(n_prompt_tokens);+ return n_prompt_tokens > n_ctx ? { status: 400, body: JSON.stringify({ error: { code: 400,+ message: `request (${n_prompt_tokens} tokens) exceeds the available context size (${n_ctx} tokens), try increasing it`,+ type: "exceed_context_size_error", n_prompt_tokens, n_ctx } }) } : null;};};+ const denseSystem = (bytes: number, messages: Array<{ role: string; content: unknown }>) =>+ tokens(bytes) + 2 * tokens(Buffer.byteLength(JSON.stringify(messages.filter((message) => message.role === "system"))));it("resends a request llama.cpp counts a few tokens over its window with most of the history kept", async () => {vi.spyOn(console, "warn").mockImplementation(() => {});@@ -275,12 +297,30 @@ describe("an OpenAI-compatible provider that refuses large requests", () => {expect(f.errors()).toEqual([]);});+ it("asks llama.cpp again with its fresh count when a dense system prompt keeps the first smaller copy over", async () => {+ vi.spyOn(console, "warn").mockImplementation(() => {});+ let window = 0;+ const f = await provider(llamaCpp((first) => window = Math.floor(first * 0.6), denseSystem));+ const done = await f.run({ system: `Fixture system prompt. ${"s".repeat(16_000)}`, transcript: transcript(20, 3_000) });+ expect(done).toMatchObject({ ok: true });+ expect(f.requests.length).toBeGreaterThan(2);+ const sent = f.requests.at(-1)!;+ expect(denseSystem(sent.bytes, sent.messages)).toBeLessThanOrEqual(window);+ expect(f.retries().map((retry) => (retry as { attempt: number }).attempt)).toEqual(f.requests.slice(1).map((_, index) => index + 1));+ const notes = sent.messages.filter((message) => typeof message.content === "string" && message.content.includes("omitted to fit"));+ expect(notes).toHaveLength(1);+ const kept = sent.messages.length - 3;+ expect(notes[0]!.content).toMatch(new RegExp(`^\\[${20 - kept} earlier messages omitted`));+ expect(f.errors()).toEqual([]);+ });+it("fails with one plain message when not even the newest message fits llama.cpp's window", async () => {vi.spyOn(console, "warn").mockImplementation(() => {});const f = await provider(llamaCpp(() => 100));const done = await f.run({ system: "Fixture system prompt.", transcript: transcript(20, 3_000) });expect(done).toMatchObject({ ok: false });expect(f.requests).toHaveLength(2);+ expect(f.retries()).toHaveLength(1);expect(f.errors()).toEqual([requestTooLargeMessage("Fixture Flash")]);});diff --git a/server/drivers/chat-size-limit.ts b/server/drivers/chat-size-limit.tsindex f8e8fadb3c..eb60d8efdb 100644--- a/server/drivers/chat-size-limit.ts+++ b/server/drivers/chat-size-limit.ts@@ -44,10 +44,19 @@ export class RequestTooLargeError extends Error {}}+/** A provider that counts each refused request may be asked again with fresh counts, since bytes and its tokens differ+ * in proportion (an image, a dense prompt); a refusal that gives no count gets one smaller copy. */+export const resendsAllowed = (refusal: RequestTooLargeError): number => refusal.fits === undefined ? 1 : 3;+export const IMAGE_OMITTED = "[image omitted to fit the provider's size limit]";export const TOOL_OUTPUT_SHORTENED = "\n[tool output shortened to fit the provider's size limit]";const omittedHistory = (count: number) =>`[${count} earlier message${count === 1 ? "" : "s"} omitted to fit the provider's size limit; full history remains in the app.]`;+const OMITTED_HISTORY = /^\[(\d+) earlier messages? omitted to fit the provider's size limit; full history remains in the app\.\]$/;+const omittedBefore = (message: OpenAIChatMessage): number | undefined => {+ const count = message.role === "assistant" && typeof message.content === "string" ? OMITTED_HISTORY.exec(message.content)?.[1] : undefined;+ return count === undefined ? undefined : Number(count);+};const LONG_TOOL_OUTPUT_BYTES = 4_000;const SHORT_TOOL_OUTPUT_BYTES = 1_000;@@ -109,12 +118,15 @@ export function smallerResend(...shrinkable.filter((index) => imagesIn(messages[index]!)).map((index) => () => replace(index, withoutImages(out[index]!))),...shrinkable.filter((index) => index > requestAt && messages[index]!.role === "tool" && sizes[index]! > LONG_TOOL_OUTPUT_BYTES).map((index) => () => replace(index, shortenedToolOutput(out[index]!))),- ...shrinkable.filter((index) => index < requestAt).map((index) => () => replace(index, null)),+ ...shrinkable.filter((index) => index < requestAt && omittedBefore(messages[index]!) === undefined).map((index) => () => replace(index, null)),];let taken = 0;for (; taken < steps.length && total > target; taken++) steps[taken]!();if (!taken) return null;- const dropped = out.slice(0, requestAt).filter((message) => message === null).length;+ const earlier = messages.slice(0, requestAt);+ const notes = earlier.flatMap((message, index) => omittedBefore(message) === undefined ? [] : [index]);+ if (notes.length && earlier.some((_, index) => out[index] === null)) for (const index of notes) replace(index, null);+ const dropped = earlier.reduce((count, message, index) => out[index] === null ? count + (omittedBefore(message) ?? 1) : count, 0);const firstDropped = out.indexOf(null);const kept = out.flatMap((message, index) => [...(dropped && index === firstDropped ? [{ role: "assistant" as const, content: omittedHistory(dropped) }] : []),diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.tsindex 5ad47200e4..7d65c67b98 100644--- a/server/drivers/openai-chat.ts+++ b/server/drivers/openai-chat.ts@@ -30,7 +30,7 @@ import { classifyError, computeBackoff, interruptibleDelay, RETRY_MAX_ATTEMPTS }import { plainFailure } from "./engine-errors.ts";import { noticeFailure } from "../chat-notices.ts";import { toolFailureMessage } from "./tool-failure.ts";-import { refusesRequestSize, RequestTooLargeError, shareThatFits, sizeChange, smallerResend } from "./chat-size-limit.ts";+import { refusesRequestSize, RequestTooLargeError, resendsAllowed, shareThatFits, sizeChange, smallerResend } from "./chat-size-limit.ts";import { requestTooLargeMessage } from "../../shared/request-too-large.ts";import { toolSchemaProfileFor } from "../tool-schema-profile.ts";import { classifyContinuable, writeTurnHandoff } from "../turn-continuation.ts";@@ -633,7 +633,7 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)for (const message of joined ?? []) emit({ ...base(turn.threadId, turnId), type: "runtime.notice", message: safeText(message) });native("out", options.nativeLog.outgoing(turn, messages, model, tools.definitions));let attempt = 0;- let resized = false;+ let resends = 0;let completion: Completion;for (;;) {let streamed = false;@@ -697,13 +697,13 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)}// A provider refuses an oversized request before running anything, so one smaller copy may go again.if (error instanceof RequestTooLargeError && !abort.signal.aborted) {- const resend = resized ? null : smallerResend(messages, request, error.bytes, error.fits);+ const resend = resends < resendsAllowed(error) ? smallerResend(messages, request, error.bytes, error.fits) : null;native("out", { refused: "request_too_large", bytes: error.bytes, ...(resend ? { before: resend.before, after: resend.after } : {}) });console.warn(`${options.driverKind}: ${model} refused a ${error.bytes}-byte request as too large; ${resend ? `resending smaller (${sizeChange(resend.before, resend.after)})` : "not resending"}`);if (!resend) throw new ChatProtocolError(requestTooLargeMessage(options.models().options.find((option) => option.id === model)?.label ?? model));messages.splice(0, messages.length, ...resend.messages);- resized = true;- emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt: 1, delayMs: 0, reason: "request_too_large" });+ resends += 1;+ emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt: resends, delayMs: 0, reason: "request_too_large" });continue;}// A refused tool call ran nothing, so the request goes again, but not once answer text streamed.