feat: add persistent model-aware reasoning effort settings
This commit is contained in:
+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);
|
||||
|
||||
Reference in New Issue
Block a user