Skip to content

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

Merge remote-tracking branch 'upstream/main' into feat/openai-compat-custom-headers

jonathanwxh-cell5ae41b62245d
diff --cc server/drivers/openai-chat.ts
index 2573adbc6e,ab9d089bf9..3472a4de75
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@@ -457,18 -457,18 +452,18 @@@ export function createOpenAIChatRuntime
signal?: AbortSignal,
onDelta?: (delta: string, kind: "assistant_text" | "reasoning_text") => void,
tools: ChatToolDefinition[] = [],
- onUsage?: TextGenerationOptions["onUsage"], variant?: string, threadId?: string,
+ onUsage?: TextGenerationOptions["onUsage"], variant?: string, threadId?: string, headerContext?: RuntimeHeaderContext,
): Promise<Completion> => {
for (;;) {
- let replied = false;
+ const attemptedRoute = routes.routeFor(model, tools.length > 0); let replied = false;
try {
- return await completeOn(routes.routeFor(model, tools.length > 0), messages, model, stream, signal, onDelta && ((text, kind) => {
+ return await completeOn(attemptedRoute, messages, model, stream, signal, onDelta && ((text, kind) => {
replied = true;
onDelta(text, kind);
- }), tools, onUsage, variant, threadId);
+ }), tools, onUsage, variant, threadId, headerContext);
} catch (error) {
if (!(error instanceof ModelRouteRefused) || replied || signal?.aborted) throw error;
- if (!routes.advance(model, error.refusal)) throw new ChatProtocolError(noRouteLeft(error.message));
+ if (routes.routeFor(model, tools.length > 0) === attemptedRoute && !routes.advance(model, error.refusal)) throw new ChatProtocolError(noRouteLeft(error.message));
console.warn(`${options.driverKind}: ${model} answered ${error.refusal}; now on ${routes.routeFor(model, true)}`);
}
}
@@@ -530,7 -529,12 +525,8 @@@
}
}
}
- const safeText = (text: string) => {
- let safe = text;
- for (const secret of secrets) if (secret) safe = safe.split(secret).join("[redacted]");
- return redactSecretsInText(safe);
- };
+ const safeText = (text: string) => redactSecretsInText(maskCustomSecrets(text, secrets));
+ const maskSecrets = (text: string, flush: boolean) => maskSecretHalves(text, secrets, safeText, flush);
const preview = (value: unknown) => toolDetailPreview(JSON.parse(JSON.stringify(value, (_key, part) =>
typeof part === "string" ? safeText(part) : part)));
const native = (dir: "out" | "in", msg: unknown) => appendNative(turn.threadId, {
@@@ -713,9 -710,9 +702,9 @@@
tools.definitions.length = 0;
continue;
}
- // Never replay a handled call. A stream closed before any completion marker ran nothing upstream, so it may resend even where the caller never opted into retries.
+ // Never resend once this call streamed (the person already saw or heard it). A stream cut ran nothing upstream, so it resends after earlier tool rounds too (their results are in the request); without a caller opt-in only the cut itself retries.
const truncation = error instanceof StreamTruncatedError, retryScale = truncation ? options.retryScale ?? 1 : options.retryScale;
- if (retryScale === undefined || abort.signal.aborted || streamed || seenCalls.size || error instanceof RuntimeNoticeError || error instanceof CustomHeaderError || (!truncation && (error instanceof ChatProtocolError || !verdict.transient)) || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;
- if (retryScale === undefined || abort.signal.aborted || streamed || error instanceof RuntimeNoticeError || (!truncation && ((error instanceof ChatProtocolError && verdict.reason !== "stream_cut") || !verdict.transient)) || (verdict.reason !== "stream_cut" && seenCalls.size) || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;
++ if (retryScale === undefined || abort.signal.aborted || streamed || error instanceof RuntimeNoticeError || error instanceof CustomHeaderError || (!truncation && ((error instanceof ChatProtocolError && verdict.reason !== "stream_cut") || !verdict.transient)) || (verdict.reason !== "stream_cut" && seenCalls.size) || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;
const delayMs = computeBackoff(attempt++);
emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt, delayMs, reason: truncation ? "stream_truncated" : verdict.reason });
await interruptibleDelay(delayMs * retryScale, abort.signal).promise;