feat: add persistent model-aware reasoning effort settings
This commit is contained in:
@@ -0,0 +1 @@
|
||||
ALTER TABLE "Chat" ADD COLUMN "reasoningEffort" TEXT;
|
||||
@@ -52,6 +52,7 @@ model Chat {
|
||||
initiatedModel String?
|
||||
lastUsedProvider String?
|
||||
lastUsedModel String?
|
||||
reasoningEffort String?
|
||||
|
||||
additionalSystemPrompt String?
|
||||
enabledTools Json?
|
||||
|
||||
@@ -12,7 +12,8 @@ import { searchBrave } from "../search/brave.js";
|
||||
import { exaClient } from "../search/exa.js";
|
||||
import { searchSearxng } from "../search/searxng.js";
|
||||
import { extractPdfText, hasPdfMagic, MAX_PDF_PAGES_PER_FETCH } from "./pdf-text.js";
|
||||
import type { ChatMessage } from "./types.js";
|
||||
import type { ChatMessage, Provider } from "./types.js";
|
||||
import type { ReasoningEffort } from "./reasoning-effort.js";
|
||||
|
||||
export const MAX_TOOL_ROUNDS = env.CHAT_MAX_TOOL_ROUNDS;
|
||||
const DEFAULT_WEB_RESULTS = 5;
|
||||
@@ -270,12 +271,14 @@ export type ToolAwareStreamingEvent =
|
||||
|
||||
export type ToolAwareCompletionParams = {
|
||||
client: any;
|
||||
provider?: Provider;
|
||||
model: string;
|
||||
messages: ChatMessage[];
|
||||
enabledTools?: string[];
|
||||
userLocation?: string;
|
||||
temperature?: number;
|
||||
maxTokens?: number;
|
||||
reasoningEffort?: ReasoningEffort | null;
|
||||
onToolEvent?: (event: ToolExecutionEvent) => void | Promise<void>;
|
||||
logContext?: {
|
||||
provider: string;
|
||||
|
||||
@@ -4,11 +4,13 @@ import {
|
||||
listModelCatalogProviders,
|
||||
} from "./provider-adapters.js";
|
||||
import type { Provider } from "./types.js";
|
||||
import { getReasoningCatalog, type ReasoningCapabilities } from "./reasoning-effort.js";
|
||||
|
||||
export type ProviderModelSnapshot = {
|
||||
models: string[];
|
||||
loadedAt: string | null;
|
||||
error: string | null;
|
||||
reasoningCapabilities?: Record<string, ReasoningCapabilities>;
|
||||
};
|
||||
|
||||
export type ModelCatalogSnapshot = Partial<Record<Provider, ProviderModelSnapshot>>;
|
||||
@@ -92,6 +94,7 @@ export function getModelCatalogSnapshot(): ModelCatalogSnapshot {
|
||||
models: [...entry.models],
|
||||
loadedAt: entry.loadedAt,
|
||||
error: entry.error,
|
||||
reasoningCapabilities: getReasoningCatalog(provider),
|
||||
};
|
||||
}
|
||||
return snapshot;
|
||||
|
||||
@@ -3,8 +3,10 @@ import { prisma } from "../db.js";
|
||||
import { buildToolLogMessageData } from "./chat-tools.js";
|
||||
import { getProviderChatAdapter } from "./provider-adapters.js";
|
||||
import type { MultiplexRequest, MultiplexResponse } from "./types.js";
|
||||
import { normalizeReasoningEffort } from "./reasoning-effort.js";
|
||||
|
||||
export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResponse> {
|
||||
req = { ...req, reasoningEffort: normalizeReasoningEffort(req.provider, req.model, req.reasoningEffort) };
|
||||
const t0 = performance.now();
|
||||
const chatId = req.chatId ?? (await prisma.chat.create({ data: {}, select: { id: true } })).id;
|
||||
|
||||
@@ -25,6 +27,7 @@ export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResp
|
||||
data: {
|
||||
lastUsedProvider: req.provider,
|
||||
lastUsedModel: req.model,
|
||||
reasoningEffort: req.reasoningEffort,
|
||||
},
|
||||
}),
|
||||
prisma.chat.updateMany({
|
||||
@@ -49,6 +52,7 @@ export async function runMultiplex(req: MultiplexRequest): Promise<MultiplexResp
|
||||
userLocation: req.userLocation,
|
||||
temperature: req.temperature,
|
||||
maxTokens: req.maxTokens,
|
||||
reasoningEffort: req.reasoningEffort,
|
||||
logContext: {
|
||||
provider: req.provider,
|
||||
model: req.model,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { getReasoningCapabilities, reasoningRequestOptions } from "../reasoning-effort.js";
|
||||
import {
|
||||
appendDanglingToolIntentCorrection,
|
||||
buildChatToolSystemPrompt,
|
||||
@@ -114,7 +115,8 @@ export async function completeWithChatCompletionsApi(params: ToolAwareCompletion
|
||||
model: params.model,
|
||||
messages: normalizePlainMessages(params.messages, params.userLocation),
|
||||
temperature: params.temperature,
|
||||
max_tokens: params.maxTokens,
|
||||
...(getReasoningCapabilities(params.provider, params.model) ? { max_completion_tokens: params.maxTokens } : { max_tokens: params.maxTokens }),
|
||||
...reasoningRequestOptions(params, "chat-completions"),
|
||||
} as any);
|
||||
|
||||
const usageAcc: Required<NonNullable<ToolAwareCompletionResult["usage"]>> = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
||||
@@ -142,7 +144,8 @@ export async function completeWithChatCompletionsApi(params: ToolAwareCompletion
|
||||
model: params.model,
|
||||
messages: conversation,
|
||||
temperature: params.temperature,
|
||||
max_tokens: params.maxTokens,
|
||||
...(getReasoningCapabilities(params.provider, params.model) ? { max_completion_tokens: params.maxTokens } : { max_tokens: params.maxTokens }),
|
||||
...reasoningRequestOptions(params, "chat-completions"),
|
||||
tools: enabledTools,
|
||||
tool_choice: "auto",
|
||||
} as any);
|
||||
@@ -227,7 +230,8 @@ export async function* streamWithChatCompletionsApi(params: ToolAwareCompletionP
|
||||
model: params.model,
|
||||
messages: normalizePlainMessages(params.messages, params.userLocation),
|
||||
temperature: params.temperature,
|
||||
max_tokens: params.maxTokens,
|
||||
...(getReasoningCapabilities(params.provider, params.model) ? { max_completion_tokens: params.maxTokens } : { max_tokens: params.maxTokens }),
|
||||
...reasoningRequestOptions(params, "chat-completions"),
|
||||
stream: true,
|
||||
} as any);
|
||||
|
||||
@@ -267,7 +271,8 @@ export async function* streamWithChatCompletionsApi(params: ToolAwareCompletionP
|
||||
model: params.model,
|
||||
messages: conversation,
|
||||
temperature: params.temperature,
|
||||
max_tokens: params.maxTokens,
|
||||
...(getReasoningCapabilities(params.provider, params.model) ? { max_completion_tokens: params.maxTokens } : { max_tokens: params.maxTokens }),
|
||||
...reasoningRequestOptions(params, "chat-completions"),
|
||||
tools: enabledTools,
|
||||
tool_choice: "auto",
|
||||
stream: true,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { reasoningRequestOptions } from "../reasoning-effort.js";
|
||||
import {
|
||||
appendDanglingToolIntentCorrection,
|
||||
buildChatToolSystemPrompt,
|
||||
@@ -155,6 +156,7 @@ export async function completeWithResponsesApi(params: ToolAwareCompletionParams
|
||||
input,
|
||||
temperature: params.temperature,
|
||||
max_output_tokens: params.maxTokens,
|
||||
...reasoningRequestOptions(params, "responses"),
|
||||
tools: toResponsesTools(enabledTools),
|
||||
tool_choice: "auto",
|
||||
parallel_tool_calls: true,
|
||||
@@ -225,6 +227,7 @@ export async function* streamWithResponsesApi(params: ToolAwareCompletionParams)
|
||||
input,
|
||||
temperature: params.temperature,
|
||||
max_output_tokens: params.maxTokens,
|
||||
...reasoningRequestOptions(params, "responses"),
|
||||
tools: toResponsesTools(enabledTools),
|
||||
tool_choice: "auto",
|
||||
parallel_tool_calls: true,
|
||||
|
||||
@@ -10,6 +10,7 @@ import { completeWithMessagesApi, streamWithMessagesApi } from "./protocols/mess
|
||||
import { completeWithResponsesApi, streamWithResponsesApi } from "./protocols/responses-api.js";
|
||||
import { anthropicClient, geminiClient, openaiClient, xaiClient } from "./providers.js";
|
||||
import type { ChatMessage, Provider } from "./types.js";
|
||||
import { normalizeReasoningEffort, type ReasoningEffort } from "./reasoning-effort.js";
|
||||
|
||||
type ProviderAdapterParams = {
|
||||
model: string;
|
||||
@@ -18,6 +19,7 @@ type ProviderAdapterParams = {
|
||||
userLocation?: string;
|
||||
temperature?: number;
|
||||
maxTokens?: number;
|
||||
reasoningEffort?: ReasoningEffort | null;
|
||||
logContext?: ToolAwareCompletionParams["logContext"];
|
||||
};
|
||||
|
||||
@@ -126,15 +128,19 @@ async function fetchJson(url: URL): Promise<any> {
|
||||
return body;
|
||||
}
|
||||
|
||||
function withClient(params: ProviderAdapterParams, client: any, enabledTools?: string[]): ToolAwareCompletionParams {
|
||||
function withClient(params: ProviderAdapterParams, provider: Provider, client: any, enabledTools?: string[]): ToolAwareCompletionParams {
|
||||
const reasoningEffort = normalizeReasoningEffort(provider, params.model, params.reasoningEffort);
|
||||
return {
|
||||
client,
|
||||
provider,
|
||||
model: params.model,
|
||||
messages: params.messages,
|
||||
enabledTools,
|
||||
userLocation: params.userLocation,
|
||||
temperature: params.temperature,
|
||||
// OpenAI reasoning efforts other than none do not accept temperature.
|
||||
temperature: reasoningEffort && reasoningEffort !== "none" ? undefined : params.temperature,
|
||||
maxTokens: params.maxTokens,
|
||||
reasoningEffort,
|
||||
logContext: params.logContext,
|
||||
};
|
||||
}
|
||||
@@ -154,11 +160,11 @@ function createProviderChatAdapter(provider: Provider, spec: ProviderBackendSpec
|
||||
provider,
|
||||
complete(params) {
|
||||
const selected = selectChatProtocol(spec, params);
|
||||
return selected.protocol.complete(withClient(params, spec.createClient(), selected.enabledTools));
|
||||
return selected.protocol.complete(withClient(params, provider, spec.createClient(), selected.enabledTools));
|
||||
},
|
||||
stream(params) {
|
||||
const selected = selectChatProtocol(spec, params);
|
||||
return selected.protocol.stream(withClient(params, spec.createClient(), selected.enabledTools));
|
||||
return selected.protocol.stream(withClient(params, provider, spec.createClient(), selected.enabledTools));
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ export type QuickQuestionRequest = {
|
||||
userLocation?: string;
|
||||
temperature?: number;
|
||||
maxTokens?: number;
|
||||
reasoningEffort?: MultiplexRequest["reasoningEffort"];
|
||||
};
|
||||
|
||||
export function buildQuickQuestionMultiplexRequest({
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
export const REASONING_EFFORTS = ["none", "minimal", "low", "medium", "high", "xhigh", "max"] as const;
|
||||
export type ReasoningEffort = (typeof REASONING_EFFORTS)[number];
|
||||
export type ReasoningCapabilities = {
|
||||
efforts: ReasoningEffort[];
|
||||
defaultEffort: ReasoningEffort | null;
|
||||
};
|
||||
|
||||
// Verified against official model pages on 2026-11-15:
|
||||
// https://developers.openai.com/api/docs/models/{model}
|
||||
// https://developers.openai.com/api/docs/guides/deployment-checklist#set-up-reasoningeffort
|
||||
// Use exact documented aliases/snapshots. Do not infer support from a prefix:
|
||||
// chat-latest, pro/codex variants, fine-tunes, and future snapshots can differ.
|
||||
const modelGroups: { models: string[]; capabilities: ReasoningCapabilities }[] = [
|
||||
{
|
||||
models: ["gpt-5", "gpt-5-2025-08-07"],
|
||||
capabilities: { efforts: ["minimal", "low", "medium", "high"], defaultEffort: "medium" },
|
||||
},
|
||||
{
|
||||
models: ["gpt-5.1", "gpt-5.1-2025-11-13"],
|
||||
capabilities: { efforts: ["none", "low", "medium", "high"], defaultEffort: "none" },
|
||||
},
|
||||
{
|
||||
models: ["gpt-5.2", "gpt-5.2-2025-12-11", "gpt-5.4", "gpt-5.4-2026-03-05",
|
||||
"gpt-5.4-mini", "gpt-5.4-mini-2026-03-17", "gpt-5.4-nano", "gpt-5.4-nano-2026-03-17"],
|
||||
capabilities: { efforts: ["none", "low", "medium", "high", "xhigh"], defaultEffort: "none" },
|
||||
},
|
||||
{
|
||||
models: ["gpt-5.5", "gpt-5.5-2026-04-23"],
|
||||
capabilities: { efforts: ["none", "low", "medium", "high", "xhigh"], defaultEffort: "medium" },
|
||||
},
|
||||
{
|
||||
models: ["gpt-5.6", "gpt-5.6-sol", "gpt-6-sol", "gpt-6-luna"],
|
||||
capabilities: { efforts: ["none", "low", "medium", "high", "xhigh", "max"], defaultEffort: "medium" },
|
||||
},
|
||||
{
|
||||
models: ["gpt-6.1-sol"],
|
||||
capabilities: { efforts: ["low", "medium", "high", "xhigh", "max"], defaultEffort: "medium" },
|
||||
},
|
||||
{
|
||||
models: ["gpt-6-astra"],
|
||||
capabilities: { efforts: ["low", "medium", "high", "xhigh", "max"], defaultEffort: null },
|
||||
},
|
||||
];
|
||||
|
||||
const openAIModels = new Map(modelGroups.flatMap(({ models, capabilities }) => models.map((model) => [model, capabilities] as const)));
|
||||
|
||||
export function getReasoningCapabilities(provider: string | null | undefined, model: string | null | undefined) {
|
||||
return provider === "openai" && model ? openAIModels.get(model) ?? null : null;
|
||||
}
|
||||
|
||||
export function getReasoningCatalog(provider: string): Record<string, ReasoningCapabilities> {
|
||||
if (provider !== "openai") return {};
|
||||
return Object.fromEntries([...openAIModels].map(([model, value]) => [model, { ...value, efforts: [...value.efforts] }]));
|
||||
}
|
||||
|
||||
export function normalizeReasoningEffort(provider: string | null | undefined, model: string | null | undefined, effort: unknown): ReasoningEffort | null {
|
||||
const capabilities = getReasoningCapabilities(provider, model);
|
||||
return capabilities?.efforts.includes(effort as ReasoningEffort) ? effort as ReasoningEffort : null;
|
||||
}
|
||||
|
||||
export function reasoningRequestOptions(params: { provider?: string; model: string; reasoningEffort?: ReasoningEffort | null }, protocol: "responses" | "chat-completions") {
|
||||
const effort = normalizeReasoningEffort(params.provider, params.model, params.reasoningEffort);
|
||||
if (!effort) return {};
|
||||
return protocol === "responses" ? { reasoning: { effort } } : { reasoning_effort: effort };
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "./chat-tools.js";
|
||||
import { getProviderChatAdapter } from "./provider-adapters.js";
|
||||
import type { MultiplexRequest, Provider } from "./types.js";
|
||||
import { normalizeReasoningEffort } from "./reasoning-effort.js";
|
||||
|
||||
type StreamUsage = {
|
||||
inputTokens?: number;
|
||||
@@ -26,6 +27,7 @@ function getChatIdOrCreate(chatId?: string) {
|
||||
}
|
||||
|
||||
export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator<StreamEvent> {
|
||||
req = { ...req, reasoningEffort: normalizeReasoningEffort(req.provider, req.model, req.reasoningEffort) };
|
||||
const t0 = performance.now();
|
||||
const shouldPersist = req.persist !== false;
|
||||
const chatId = shouldPersist ? await getChatIdOrCreate(req.chatId) : null;
|
||||
@@ -50,6 +52,7 @@ export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator
|
||||
data: {
|
||||
lastUsedProvider: req.provider,
|
||||
lastUsedModel: req.model,
|
||||
reasoningEffort: req.reasoningEffort,
|
||||
},
|
||||
}),
|
||||
prisma.chat.updateMany({
|
||||
@@ -77,6 +80,7 @@ export async function* runMultiplexStream(req: MultiplexRequest): AsyncGenerator
|
||||
userLocation: req.userLocation,
|
||||
temperature: req.temperature,
|
||||
maxTokens: req.maxTokens,
|
||||
reasoningEffort: req.reasoningEffort,
|
||||
logContext: {
|
||||
provider: req.provider,
|
||||
model: req.model,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { ReasoningEffort } from "./reasoning-effort.js";
|
||||
|
||||
export const PROVIDERS = ["openai", "anthropic", "xai", "gemini"] as const;
|
||||
|
||||
export type Provider = (typeof PROVIDERS)[number];
|
||||
@@ -42,6 +44,7 @@ export type MultiplexRequest = {
|
||||
userLocation?: string;
|
||||
temperature?: number;
|
||||
maxTokens?: number;
|
||||
reasoningEffort?: ReasoningEffort | null;
|
||||
};
|
||||
|
||||
export type MultiplexResponse = {
|
||||
|
||||
+41
-11
@@ -16,6 +16,7 @@ import { runMultiplexStream, type StreamEvent } from "./llm/streaming.js";
|
||||
import { getAvailableChatTools, normalizeEnabledChatTools } from "./llm/chat-tools.js";
|
||||
import { getModelCatalogSnapshot } from "./llm/model-catalog.js";
|
||||
import { buildQuickQuestionMultiplexRequest } from "./llm/quick-question.js";
|
||||
import { normalizeReasoningEffort, REASONING_EFFORTS, type ReasoningEffort } from "./llm/reasoning-effort.js";
|
||||
import { openaiClient } from "./llm/providers.js";
|
||||
import { serializeProviderFields } from "./llm/provider-ids.js";
|
||||
import { exaClient } from "./search/exa.js";
|
||||
@@ -24,6 +25,7 @@ import { searchWorkspace } from "./workspace-search.js";
|
||||
import { PROVIDERS, type ChatAttachment } from "./llm/types.js";
|
||||
|
||||
const ProviderSchema = z.enum(PROVIDERS);
|
||||
const ReasoningEffortSchema = z.enum(REASONING_EFFORTS).nullable();
|
||||
const MAX_ADDITIONAL_SYSTEM_PROMPT_CHARS = 12_000;
|
||||
const MAX_FORK_MESSAGE_SNIPPET_CHARS = 48;
|
||||
const EnabledToolsSchema = z.array(z.string().trim().min(1).max(80)).max(20).transform((value) => normalizeEnabledChatTools(value));
|
||||
@@ -209,6 +211,7 @@ const CompletionStreamBody = z
|
||||
userLocation: z.string().trim().min(1).max(200).optional(),
|
||||
temperature: z.number().min(0).max(2).optional(),
|
||||
maxTokens: z.number().int().positive().optional(),
|
||||
reasoningEffort: ReasoningEffortSchema.optional(),
|
||||
})
|
||||
.superRefine((value, ctx) => {
|
||||
if (value.persist === false && value.chatId) {
|
||||
@@ -235,6 +238,7 @@ const QuickQuestionStreamBody = z.object({
|
||||
userLocation: z.string().trim().min(1).max(200).optional(),
|
||||
temperature: z.number().min(0).max(2).optional(),
|
||||
maxTokens: z.number().int().positive().optional(),
|
||||
reasoningEffort: ReasoningEffortSchema.optional(),
|
||||
});
|
||||
|
||||
function mergeAttachmentsIntoMetadata(metadata: unknown, attachments?: ChatAttachment[]) {
|
||||
@@ -269,16 +273,16 @@ function prependAdditionalSystemPrompt<T extends { messages: IncomingChatMessage
|
||||
};
|
||||
}
|
||||
|
||||
async function applyStoredChatSettings<T extends { chatId?: string; messages: IncomingChatMessage[]; additionalSystemPrompt?: string; enabledTools?: string[] }>(
|
||||
async function applyStoredChatSettings<T extends { chatId?: string; provider: string; model: string; messages: IncomingChatMessage[]; additionalSystemPrompt?: string; enabledTools?: string[]; reasoningEffort?: ReasoningEffort | null }>(
|
||||
body: T
|
||||
) {
|
||||
if (!body.chatId || (body.additionalSystemPrompt !== undefined && body.enabledTools !== undefined)) {
|
||||
if (!body.chatId || (body.additionalSystemPrompt !== undefined && body.enabledTools !== undefined && body.reasoningEffort !== undefined)) {
|
||||
return prependAdditionalSystemPrompt(body);
|
||||
}
|
||||
|
||||
const chat = await prisma.chat.findUnique({
|
||||
where: { id: body.chatId },
|
||||
select: { additionalSystemPrompt: true, enabledTools: true },
|
||||
select: { additionalSystemPrompt: true, enabledTools: true, reasoningEffort: true, lastUsedProvider: true, lastUsedModel: true },
|
||||
});
|
||||
if (!chat) return prependAdditionalSystemPrompt(body);
|
||||
|
||||
@@ -286,6 +290,10 @@ async function applyStoredChatSettings<T extends { chatId?: string; messages: In
|
||||
...body,
|
||||
additionalSystemPrompt: body.additionalSystemPrompt ?? chat.additionalSystemPrompt ?? undefined,
|
||||
enabledTools: body.enabledTools ?? normalizeEnabledChatTools(chat.enabledTools),
|
||||
// Omission inherits only for the saved model; null explicitly resets it.
|
||||
reasoningEffort: normalizeReasoningEffort(body.provider, body.model, body.reasoningEffort !== undefined
|
||||
? body.reasoningEffort
|
||||
: chat.lastUsedProvider === body.provider && chat.lastUsedModel === body.model ? chat.reasoningEffort : null),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -556,6 +564,7 @@ const chatSummarySelect = {
|
||||
initiatedModel: true,
|
||||
lastUsedProvider: true,
|
||||
lastUsedModel: true,
|
||||
reasoningEffort: true,
|
||||
additionalSystemPrompt: true,
|
||||
enabledTools: true,
|
||||
projectItems: starredProjectItemsSelect,
|
||||
@@ -1082,7 +1091,7 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
} : undefined,
|
||||
orderBy: [{ updatedAt: "desc" }, { id: "asc" }],
|
||||
take: limit + 1,
|
||||
select: { id: true, title: true, parentChatId: true, createdAt: true, updatedAt: true, initiatedProvider: true, initiatedModel: true, lastUsedProvider: true, lastUsedModel: true },
|
||||
select: { id: true, title: true, parentChatId: true, createdAt: true, updatedAt: true, initiatedProvider: true, initiatedModel: true, lastUsedProvider: true, lastUsedModel: true, reasoningEffort: true },
|
||||
});
|
||||
const hasMore = chats.length > limit;
|
||||
const page = hasMore ? chats.slice(0, limit) : chats;
|
||||
@@ -1103,6 +1112,7 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
title: z.string().optional(),
|
||||
provider: ProviderSchema.optional(),
|
||||
model: z.string().trim().min(1).optional(),
|
||||
reasoningEffort: ReasoningEffortSchema.optional(),
|
||||
additionalSystemPrompt: z.string().max(MAX_ADDITIONAL_SYSTEM_PROMPT_CHARS).optional(),
|
||||
enabledTools: EnabledToolsSchema.optional(),
|
||||
messages: z.array(CompletionMessageSchema).optional(),
|
||||
@@ -1133,6 +1143,7 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
initiatedModel: body.model,
|
||||
lastUsedProvider: body.provider,
|
||||
lastUsedModel: body.model,
|
||||
reasoningEffort: normalizeReasoningEffort(body.provider, body.model, body.reasoningEffort),
|
||||
additionalSystemPrompt: normalizeAdditionalSystemPrompt(body.additionalSystemPrompt),
|
||||
enabledTools: body.enabledTools as any,
|
||||
messages: body.messages?.length
|
||||
@@ -1170,6 +1181,7 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
initiatedModel: true,
|
||||
lastUsedProvider: true,
|
||||
lastUsedModel: true,
|
||||
reasoningEffort: true,
|
||||
additionalSystemPrompt: true,
|
||||
enabledTools: true,
|
||||
userId: true,
|
||||
@@ -1199,6 +1211,7 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
initiatedModel: source.initiatedModel,
|
||||
lastUsedProvider: source.lastUsedProvider,
|
||||
lastUsedModel: source.lastUsedModel,
|
||||
reasoningEffort: normalizeReasoningEffort(source.lastUsedProvider, source.lastUsedModel, source.reasoningEffort),
|
||||
additionalSystemPrompt: source.additionalSystemPrompt,
|
||||
enabledTools: (source.enabledTools ?? undefined) as any,
|
||||
userId: source.userId,
|
||||
@@ -1231,11 +1244,16 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
const Params = z.object({ chatId: z.string() });
|
||||
const Body = z.object({
|
||||
title: z.string().trim().min(1).optional(),
|
||||
provider: ProviderSchema.optional(),
|
||||
model: z.string().trim().min(1).optional(),
|
||||
reasoningEffort: ReasoningEffortSchema.optional(),
|
||||
additionalSystemPrompt: z.string().max(MAX_ADDITIONAL_SYSTEM_PROMPT_CHARS).nullable().optional(),
|
||||
enabledTools: EnabledToolsSchema.optional(),
|
||||
});
|
||||
}).refine((value) => Boolean(value.provider) === Boolean(value.model), "provider and model must be supplied together");
|
||||
const { chatId } = Params.parse(req.params);
|
||||
const body = Body.parse(req.body ?? {});
|
||||
const parsed = Body.safeParse(req.body ?? {});
|
||||
if (!parsed.success) return app.httpErrors.badRequest(parsed.error.message);
|
||||
const body = parsed.data;
|
||||
|
||||
const data: Record<string, unknown> = {};
|
||||
if (body.title !== undefined) {
|
||||
@@ -1244,13 +1262,24 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
}
|
||||
if (body.additionalSystemPrompt !== undefined) data.additionalSystemPrompt = normalizeAdditionalSystemPrompt(body.additionalSystemPrompt);
|
||||
if (body.enabledTools !== undefined) data.enabledTools = body.enabledTools;
|
||||
|
||||
const updated = await prisma.chat.updateMany({
|
||||
where: { id: chatId },
|
||||
data: data as any,
|
||||
const updated = await prisma.$transaction(async (tx) => {
|
||||
const current = await tx.chat.findUnique({ where: { id: chatId } });
|
||||
if (!current) return null;
|
||||
const provider = body.provider ?? current.lastUsedProvider;
|
||||
const model = body.model ?? current.lastUsedModel;
|
||||
if (body.provider !== undefined) {
|
||||
data.lastUsedProvider = provider;
|
||||
data.lastUsedModel = model;
|
||||
}
|
||||
if (body.provider !== undefined || body.reasoningEffort !== undefined) {
|
||||
const sameModel = provider === current.lastUsedProvider && model === current.lastUsedModel;
|
||||
data.reasoningEffort = normalizeReasoningEffort(provider, model, body.reasoningEffort !== undefined
|
||||
? body.reasoningEffort : sameModel ? current.reasoningEffort : null);
|
||||
}
|
||||
return tx.chat.update({ where: { id: chatId }, data: data as any });
|
||||
});
|
||||
|
||||
if (updated.count === 0) return app.httpErrors.notFound("chat not found");
|
||||
if (!updated) return app.httpErrors.notFound("chat not found");
|
||||
|
||||
const chat = await getChatSummary(chatId);
|
||||
if (!chat) return app.httpErrors.notFound("chat not found");
|
||||
@@ -1738,6 +1767,7 @@ export async function registerRoutes(app: FastifyInstance, options: { agentInlin
|
||||
userLocation: z.string().trim().min(1).max(200).optional(),
|
||||
temperature: z.number().min(0).max(2).optional(),
|
||||
maxTokens: z.number().int().positive().optional(),
|
||||
reasoningEffort: ReasoningEffortSchema.optional(),
|
||||
});
|
||||
|
||||
const parsed = Body.safeParse(req.body);
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import Fastify from "fastify";
|
||||
import sensible from "@fastify/sensible";
|
||||
import { getReasoningCatalog, normalizeReasoningEffort } from "../src/llm/reasoning-effort.js";
|
||||
|
||||
const serverRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const databaseDir = mkdtempSync(join(tmpdir(), "sybil-reasoning-"));
|
||||
process.env.DATABASE_URL = `file:${join(databaseDir, "test.db")}`;
|
||||
writeFileSync(join(databaseDir, "test.db"), "");
|
||||
process.env.OPENAI_API_KEY = "test-key";
|
||||
process.env.XAI_API_KEY = "test-key";
|
||||
delete process.env.ADMIN_TOKEN;
|
||||
execFileSync(process.execPath, [join(serverRoot, "node_modules/prisma/build/index.js"), "migrate", "deploy"], {
|
||||
cwd: serverRoot, env: { ...process.env, PRISMA_HIDE_UPDATE_MESSAGE: "1" }, stdio: "pipe",
|
||||
});
|
||||
const [{ registerRoutes }, { prisma }, { getProviderChatAdapter }] = await Promise.all([
|
||||
import("../src/routes.js"), import("../src/db.js"), import("../src/llm/provider-adapters.js"),
|
||||
]);
|
||||
const app = Fastify({ logger: false });
|
||||
await app.register(sensible);
|
||||
await registerRoutes(app);
|
||||
await app.ready();
|
||||
|
||||
const requests: { path: string; body: any }[] = [];
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (input, init) => {
|
||||
const url = new URL(input instanceof Request ? input.url : String(input));
|
||||
assert.ok(["api.openai.com", "api.x.ai"].includes(url.hostname), `Unexpected network request: ${url.origin}`);
|
||||
const body = JSON.parse(String(init?.body));
|
||||
requests.push({ path: url.pathname, body });
|
||||
const message = { id: "message", type: "message", role: "assistant", content: [{ type: "output_text", text: "Mock answer." }] };
|
||||
const response = { id: "response", status: "completed", output: [message], output_text: "Mock answer." };
|
||||
if (!body.stream) return Response.json(url.pathname.endsWith("/responses") ? response : {
|
||||
id: "completion", choices: [{ message: { role: "assistant", content: "Mock answer." }, finish_reason: "stop" }],
|
||||
});
|
||||
const events = url.pathname.endsWith("/responses") ? [
|
||||
{ type: "response.output_item.added", item: message, output_index: 0 },
|
||||
{ type: "response.output_text.delta", delta: "Mock answer.", output_index: 0 },
|
||||
{ type: "response.completed", response },
|
||||
] : [{ choices: [{ index: 0, delta: { content: "Mock answer." }, finish_reason: "stop" }] }];
|
||||
return new Response(events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join("") + "data: [DONE]\n\n", {
|
||||
headers: { "Content-Type": "text/event-stream" },
|
||||
});
|
||||
};
|
||||
test.after(async () => {
|
||||
globalThis.fetch = originalFetch;
|
||||
await app.close();
|
||||
await prisma.$disconnect();
|
||||
rmSync(databaseDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test("capabilities require an exact verified provider/model/effort combination", () => {
|
||||
assert.equal(normalizeReasoningEffort("openai", "gpt-5.2", "xhigh"), "xhigh");
|
||||
assert.equal(normalizeReasoningEffort("openai", "gpt-5.2-2025-12-11", "none"), "none");
|
||||
assert.equal(normalizeReasoningEffort("openai", "gpt-5", "minimal"), "minimal");
|
||||
for (const [provider, model, effort] of [
|
||||
["openai", "gpt-5.1", "xhigh"], ["openai", "gpt-6.1-sol", "none"],
|
||||
["openai", "gpt-5.2-future", "high"], ["openai", "gpt-5.2-pro", "high"],
|
||||
["openai", "ft:gpt-5.2", "high"], ["openai", "gpt-4.1-mini", "high"],
|
||||
["openai", "chat-latest", "high"], ["xai", "gpt-5.2", "high"],
|
||||
]) assert.equal(normalizeReasoningEffort(provider, model, effort), null);
|
||||
const catalog = getReasoningCatalog("openai");
|
||||
assert.equal(catalog["gpt-5.2"].defaultEffort, "none");
|
||||
assert.equal(catalog["gpt-5.5"].defaultEffort, "medium");
|
||||
assert.deepEqual(getReasoningCatalog("anthropic"), {});
|
||||
catalog["gpt-5.2"].efforts.push("max");
|
||||
assert.equal(normalizeReasoningEffort("openai", "gpt-5.2", "max"), null);
|
||||
});
|
||||
|
||||
test("real SDK serialization uses the right effort field for every OpenAI protocol and stream mode", async () => {
|
||||
for (const enabledTools of [[], ["web_search"]]) {
|
||||
for (const streaming of [false, true]) {
|
||||
for (const reasoningEffort of ["high", "none", null] as const) {
|
||||
const adapter = getProviderChatAdapter("openai");
|
||||
const params = { model: "gpt-5.2", messages: [{ role: "user" as const, content: "Hello" }], enabledTools, reasoningEffort, temperature: 0.2, maxTokens: 1000 };
|
||||
if (streaming) {
|
||||
const events = [];
|
||||
for await (const event of adapter.stream(params)) events.push(event);
|
||||
assert.equal(events.at(-1)?.type, "done");
|
||||
} else assert.equal((await adapter.complete(params)).text, "Mock answer.");
|
||||
const { path, body } = requests.at(-1)!;
|
||||
assert.equal(path, enabledTools.length ? "/v1/responses" : "/v1/chat/completions");
|
||||
if (enabledTools.length) {
|
||||
assert.deepEqual(body.reasoning, reasoningEffort ? { effort: reasoningEffort } : undefined);
|
||||
assert.equal(body.reasoning_effort, undefined);
|
||||
assert.equal(body.max_output_tokens, 1000);
|
||||
} else {
|
||||
assert.equal(body.reasoning_effort, reasoningEffort ?? undefined);
|
||||
assert.equal(body.reasoning, undefined);
|
||||
assert.equal(body.max_completion_tokens, 1000);
|
||||
assert.equal(body.max_tokens, undefined);
|
||||
}
|
||||
assert.equal(body.temperature, reasoningEffort === "high" ? undefined : 0.2);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("unsupported models, unsupported efforts and other providers never receive an effort field", async () => {
|
||||
for (const provider of ["openai", "xai"] as const) {
|
||||
for (const model of ["gpt-4.1-mini", "chat-latest", "gpt-5.1", "gpt-5.2-unknown"]) {
|
||||
for (const enabledTools of [[], ["web_search"]]) {
|
||||
const adapter = getProviderChatAdapter(provider);
|
||||
await adapter.complete({ model, messages: [{ role: "user", content: "Hi" }], enabledTools, reasoningEffort: "xhigh" });
|
||||
const { body } = requests.at(-1)!;
|
||||
assert.equal("reasoning" in body, false);
|
||||
assert.equal("reasoning_effort" in body, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("chat settings persist, survive all summary/detail reads and forks, and reset on a model change", async () => {
|
||||
const created = await app.inject({ method: "POST", url: "/v1/chats", payload: { provider: "openai", model: "gpt-5.2", reasoningEffort: "high" } });
|
||||
assert.equal(created.statusCode, 200, created.body);
|
||||
const chat = created.json().chat;
|
||||
assert.equal(chat.reasoningEffort, "high");
|
||||
for (const url of [`/v1/chats/${chat.id}`, "/v1/chats", "/v1/chats?limit=10", "/v1/workspace-items"]) {
|
||||
const body = (await app.inject({ method: "GET", url })).json();
|
||||
const stored = body.chat ?? (body.chats ?? body.items).find((item: any) => item.id === chat.id);
|
||||
assert.equal(stored.reasoningEffort, "high", url);
|
||||
}
|
||||
const fork = await app.inject({ method: "POST", url: `/v1/chats/${chat.id}/fork`, payload: {} });
|
||||
assert.equal(fork.json().chat.reasoningEffort, "high");
|
||||
const patch = async (payload: object) => app.inject({ method: "PATCH", url: `/v1/chats/${chat.id}`, payload });
|
||||
assert.equal((await patch({ title: "Renamed" })).json().chat.reasoningEffort, "high");
|
||||
const switched = (await patch({ provider: "openai", model: "gpt-5.1" })).json().chat;
|
||||
assert.equal(switched.lastUsedModel, "gpt-5.1");
|
||||
assert.equal(switched.reasoningEffort, null);
|
||||
assert.equal((await patch({ reasoningEffort: "xhigh" })).json().chat.reasoningEffort, null);
|
||||
assert.equal((await patch({ reasoningEffort: "low" })).json().chat.reasoningEffort, "low");
|
||||
assert.equal((await patch({ reasoningEffort: null })).json().chat.reasoningEffort, null);
|
||||
assert.equal((await patch({ model: "gpt-5.2" })).statusCode, 400);
|
||||
assert.equal((await patch({ reasoningEffort: "bogus" })).statusCode, 400);
|
||||
const catalog = (await app.inject({ method: "GET", url: "/v1/models" })).json();
|
||||
assert.deepEqual(catalog.providers.openai.reasoningCapabilities["gpt-5.2"].efforts, ["none", "low", "medium", "high", "xhigh"]);
|
||||
});
|
||||
|
||||
test("saved effort is inherited by older clients, overridden/reset explicitly, and never carried to another model", async () => {
|
||||
for (const streaming of [false, true]) {
|
||||
const chat = await prisma.chat.create({ data: { lastUsedProvider: "openai", lastUsedModel: "gpt-5.2", reasoningEffort: "high", enabledTools: [] } });
|
||||
const send = async (extra: object) => {
|
||||
const response = await app.inject({ method: "POST", url: `/v1/chat-completions${streaming ? "/stream" : ""}`,
|
||||
payload: { chatId: chat.id, provider: "openai", model: "gpt-5.2", messages: [{ role: "user", content: "Hello" }], ...extra } });
|
||||
assert.equal(response.statusCode, 200, response.body);
|
||||
assert.doesNotMatch(response.body, /event: error/);
|
||||
return requests.at(-1)!.body;
|
||||
};
|
||||
assert.equal((await send({})).reasoning_effort, "high");
|
||||
assert.equal((await send({ reasoningEffort: "low" })).reasoning_effort, "low");
|
||||
assert.equal((await prisma.chat.findUniqueOrThrow({ where: { id: chat.id } })).reasoningEffort, "low");
|
||||
assert.equal((await send({ reasoningEffort: null })).reasoning_effort, undefined);
|
||||
await send({ reasoningEffort: "high" });
|
||||
assert.equal((await send({ model: "gpt-5.1" })).reasoning_effort, undefined);
|
||||
assert.equal((await prisma.chat.findUniqueOrThrow({ where: { id: chat.id } })).reasoningEffort, null);
|
||||
}
|
||||
});
|
||||
|
||||
test("Quick Questions accept validated effort without inheriting or persisting chat settings", async () => {
|
||||
const count = await prisma.chat.count();
|
||||
for (const [model, effort, expected] of [["gpt-5.2", "high", "high"], ["gpt-5.1", "xhigh", undefined], ["chat-latest", "high", undefined], ["gpt-5.2", undefined, undefined]] as const) {
|
||||
const response = await app.inject({ method: "POST", url: "/v1/quick-questions/stream", payload: { provider: "openai", model, question: "Hello", enabledTools: [], reasoningEffort: effort } });
|
||||
assert.equal(response.statusCode, 200, response.body);
|
||||
assert.doesNotMatch(response.body, /event: error/);
|
||||
assert.equal(requests.at(-1)!.body.reasoning_effort, expected);
|
||||
}
|
||||
assert.equal(await prisma.chat.count(), count);
|
||||
});
|
||||
Reference in New Issue
Block a user