Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
OpenAI-compatible: an older llama.cpp's streamed refusal resends smaller, and a left-out tool output never asks for a rerun
Absterrg01db963e01e95
diff --git a/server/drivers/chat-size-limit.test.ts b/server/drivers/chat-size-limit.test.tsindex bb7abb77e9..cd694c8543 100644--- a/server/drivers/chat-size-limit.test.ts+++ b/server/drivers/chat-size-limit.test.ts@@ -211,7 +211,7 @@ describe("an OpenAI-compatible provider that refuses large requests", () => {requests.push({ bytes: raw.length, messages });const refusal = refuse(raw.length, messages);if (refusal) {- res.writeHead(refusal.status, { "content-type": "text/html" });+ res.writeHead(refusal.status, { "content-type": refusal.status === 200 ? "text/event-stream" : "text/html" });res.end(refusal.body);return;}@@ -282,14 +282,17 @@ describe("an OpenAI-compatible provider that refuses large requests", () => {});const tokens = (bytes: number) => Math.ceil(bytes / 4);- const llamaCpp = (window: (firstTokens: number) => number, count: (bytes: number, messages: Array<{ role: string; content: unknown }>) => number = tokens) => {+ /** llama.cpp b7200 on answers HTTP 400; b6500 streamed HTTP 200 with an SSE `error:` event holding the bare error, then [DONE]. */+ const llamaCpp = (window: (firstTokens: number) => number, count: (bytes: number, messages: Array<{ role: string; content: unknown }>) => number = tokens,+ delivery: "http" | "sse" = "http") => {let n_ctx: number | undefined;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;+ if (n_prompt_tokens < n_ctx) return null;+ const 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 };+ return delivery === "http" ? { status: 400, body: JSON.stringify({ error }) } : { status: 200, body: `error: ${JSON.stringify(error)}\n\ndata: [DONE]\n\n` };};};const denseSystem = (bytes: number, messages: Array<{ role: string; content: unknown }>) =>@@ -310,6 +313,30 @@ describe("an OpenAI-compatible provider that refuses large requests", () => {expect(f.errors()).toEqual([]);});+ it("resends a request an older llama.cpp refuses in its stream's error event, with most of the history kept", async () => {+ vi.spyOn(console, "warn").mockImplementation(() => {});+ const f = await provider(llamaCpp((first) => first - 2, tokens, "sse"));+ const done = await f.run({ system: "Fixture system prompt.", transcript: transcript(20, 3_000) });+ expect(done).toMatchObject({ ok: true });+ expect(f.requests).toHaveLength(2);+ const [refused, resent] = f.requests;+ expect(resent!.bytes).toBeLessThanOrEqual(refused!.bytes * 0.8);+ expect(resent!.bytes).toBeGreaterThan(refused!.bytes * 0.6);+ expect(f.retries()).toEqual([expect.objectContaining({ attempt: 1, reason: "request_too_large" })]);+ expect(f.errors()).toEqual([]);+ });++ it.each([+ ['{"code":400,"message":"invalid grammar","type":"invalid_request_error"}', "upstream HTTP 400: invalid grammar"],+ ["slot unavailable", "provider returned a streaming error: slot unavailable"],+ ])("ends on the provider's own words for a streamed error event that is not about size: %s", async (event, message) => {+ const f = await provider(() => ({ status: 200, body: `error: ${event}\n\ndata: [DONE]\n\n` }));+ const done = await f.run({ transcript: transcript(2, 100) });+ expect(done).toMatchObject({ ok: false });+ expect(f.requests).toHaveLength(1);+ expect(f.errors()).toEqual([message]);+ });+it("resends a request llama.cpp counts at three times its window small enough to fit, where half would not", async () => {vi.spyOn(console, "warn").mockImplementation(() => {});const f = await provider(llamaCpp((first) => Math.floor(first / 3)));diff --git a/server/drivers/chat-size-limit.ts b/server/drivers/chat-size-limit.tsindex 51efd89cfe..0087e32c8b 100644--- a/server/drivers/chat-size-limit.ts+++ b/server/drivers/chat-size-limit.ts@@ -51,7 +51,7 @@ export const resendsAllowed = (refusal: RequestTooLargeError): number => refusalexport 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]";-export const TOOL_OUTPUT_OMITTED = "[earlier tool output omitted to fit the provider's size limit; run the tool again if it is still needed]";+export const TOOL_OUTPUT_OMITTED = "[earlier tool output omitted to fit the provider's size limit. The call already ran: do not repeat it to see this output; if you need what it did, check the current state first.]";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\.\]$/;diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.tsindex 1c95bbc772..571d003854 100644--- a/server/drivers/openai-chat.ts+++ b/server/drivers/openai-chat.ts@@ -373,7 +373,15 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)const reader = response.body.getReader();const decoder = new TextDecoder();let buffer = "";+ // llama.cpp before b7200 (seen on b6500) refuses a streamed request with HTTP 200 and an SSE `error:` event holding the bare error.+ const streamedFailure = (data: string): Error => {+ let error: Record<string, unknown> | undefined;+ try { error = object(JSON.parse(data)); } catch { error = undefined; }+ return typeof error?.code === "number" ? failure(error.code, JSON.stringify({ error }))+ : new ChatProtocolError(`provider returned a streaming error: ${data.slice(0, 200)}`);+ };const consumeDataLine = (line: string, atEof = false): boolean => {+ if (line.startsWith("error:")) throw streamedFailure(line.slice("error:".length).trim());if (!line.startsWith("data:")) return false;const data = line.slice(5).trim();if (data === "[DONE]") return true;