Skip to content

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

Merge remote-tracking branch 'origin/main' into train/nov18-w4

milind-soni5a68d971dc18
diff --cc server/drivers/openai-chat.ts
index 2e1ee2cf1e,1fedf7e4c9..1b6b611a61
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@@ -35,8 -35,9 +35,8 @@@ import { requestTooLargeMessage } from
import { toolSchemaProfileFor } from "../tool-schema-profile.ts";
import { classifyContinuable, writeTurnHandoff } from "../turn-continuation.ts";
import { KEY_REJECTED_REASON, keyRejected, noteKeyAccepted, noteKeyRejected, rejectsKey } from "../key-rejections.ts";
- import { FETCH_LIMIT_MS, fetchPastLimit } from "./openai-chat-fetch.ts";
+ import { FETCH_LIMIT_MS, fetchPastLimit, prepareChatModel } from "./openai-chat-fetch.ts";
import { announcesAction } from "./chat-announcement.ts";
-
export interface OpenAIChatMessage {
role: "system" | "user" | "assistant" | "tool";
content: string | ChatContentPart[] | null;
@@@ -46,11 -47,13 +46,11 @@@
reasoning?: string;
reasoning_details?: Record<string, unknown>[];
}
--
interface Usage {
input: number;
output: number;
+ cachedInput?: number;
}
-
export interface Completion {
text: string;
reasoning: string;
@@@ -62,7 -65,7 +62,6 @@@
/** Every output item of a Responses API reply, encrypted reasoning included, for the next round of this turn only. */
responseOutput?: ResponseItem[];
}
--
interface CompletionJson extends ProviderBody {
model?: string;
choices?: Array<{
@@@ -75,7 -78,7 +74,6 @@@
/** Groq reports streaming usage in its final x_groq envelope. */
x_groq?: { usage?: CompletionJson["usage"] };
}
--
interface NativeLog {
source: string;
outgoing(turn: SendTurnInput, messages: OpenAIChatMessage[], model: string, tools: ChatToolDefinition[]): unknown;
@@@ -129,11 -131,14 +127,13 @@@ interface RuntimeOptions<Config>
* to fix a failed call instead of ending the turn on it. */
localModel?: boolean;
}
--
- const usageFrom = (usage: CompletionJson["usage"]): Usage | null =>
- usage
- ? { input: usage.prompt_tokens ?? 0, output: usage.completion_tokens ?? 0 }
+ const usageFrom = (usage: CompletionJson["usage"]): Usage | null => {
+ const cachedInput = usage?.prompt_tokens_details?.cached_tokens;
+ return usage
+ ? { input: usage.prompt_tokens ?? 0, output: usage.completion_tokens ?? 0,
+ ...(typeof cachedInput === "number" && Number.isFinite(cachedInput) && cachedInput >= 0 ? { cachedInput } : {}) }
: null;
+ };
const completionUsage = (json: CompletionJson) => json.usage ?? json.x_groq?.usage;
@@@ -276,13 -281,11 +276,13 @@@ export function createOpenAIChatRuntime
? AbortSignal.any([signal, timeoutController.signal])
: timeoutController.signal;
- const served = options.servedModel ? await options.servedModel(model) : model;
+ const served = options.servedModel ? await prepareChatModel(() => options.servedModel!(model), activeSignal) : model;
+ const requestBody = options.requestBody(served, messages, stream, variant, threadId, model);
+ const effort = selectedReasoningEffort(requestBody, route, variant, options.selectedReasoning?.(model, variant));
const payload = JSON.stringify(route === "responses"
- ? responsesRequest({ model: served, messages, tools, replay: (message) => responseOutputs.get(message), summaries: routes.summaries(model), effort: options.requestBody(served, messages, stream, variant).reasoning_effort })
+ ? responsesRequest({ model: served, messages, tools, replay: (message) => responseOutputs.get(message), summaries: routes.summaries(model), effort })
: {
- ...options.requestBody(served, messages, stream, variant, threadId),
+ ...requestBody,
...(route === "chat-reasoning-off" ? (options.reasoningOffBody?.(served) ?? { reasoning_effort: "none" }) : {}),
...(tools.length ? { tools } : {}),
});