fix stuff
This commit is contained in:
@@ -271,8 +271,6 @@ export function createChatService(repositories, modelService, config) {
|
||||
|
||||
const payload = this.buildMessagesPayload({user, chatId, message});
|
||||
|
||||
insertChatMessage(chatId, "user", message);
|
||||
repositories.touchChat(chatId, user);
|
||||
const activePersona = payload.activePersona;
|
||||
const activeUserPersona = payload.activeUserPersona;
|
||||
const messagesPayload = payload.messagesPayload;
|
||||
@@ -283,6 +281,7 @@ export function createChatService(repositories, modelService, config) {
|
||||
|
||||
if (!fullReply) return {error: "Failed to generate a valid character reply", status: 500};
|
||||
|
||||
insertChatMessage(chatId, "user", message);
|
||||
insertChatMessage(chatId, "bot", fullReply, {
|
||||
retryVariants: [fullReply],
|
||||
retryActiveIndex: 0,
|
||||
@@ -299,20 +298,19 @@ export function createChatService(repositories, modelService, config) {
|
||||
|
||||
const payload = this.buildMessagesPayload({user, chatId, message});
|
||||
|
||||
insertChatMessage(chatId, "user", message);
|
||||
repositories.touchChat(chatId, user);
|
||||
const activePersona = payload.activePersona;
|
||||
const activeUserPersona = payload.activeUserPersona;
|
||||
const messagesPayload = payload.messagesPayload;
|
||||
|
||||
// Persona replies must be validated before any text is exposed to the client.
|
||||
// Normal chats can retain true incremental streaming.
|
||||
const fullReply = activePersona
|
||||
? await modelService.streamReply(model, messagesPayload, onChunk)
|
||||
? await generateValidatedRoleplayReply({selectedModel: model, messagesPayload, userPersona: activeUserPersona})
|
||||
: await modelService.streamReply(model, messagesPayload, onChunk);
|
||||
|
||||
if (!fullReply) return {error: "Failed to generate a valid character reply", status: 500};
|
||||
if (activePersona && containsUserVoiceInRoleplayOpener(fullReply, activeUserPersona)) {
|
||||
return {error: "Failed to generate a valid character reply", status: 500};
|
||||
}
|
||||
if (activePersona && onChunk) onChunk(fullReply, fullReply);
|
||||
insertChatMessage(chatId, "user", message);
|
||||
insertChatMessage(chatId, "bot", fullReply, {
|
||||
retryVariants: [fullReply],
|
||||
retryActiveIndex: 0,
|
||||
@@ -327,10 +325,14 @@ export function createChatService(repositories, modelService, config) {
|
||||
const session = repositories.getChat(chatId, user);
|
||||
if (!session) return {error: "Chat not found", status: 404};
|
||||
|
||||
const existingMessage = formatChatMessage(targetMessage);
|
||||
if (existingMessage.retryRetriesUsed >= 5) {
|
||||
return {error: "Maximum of 5 retries reached", status: 400};
|
||||
}
|
||||
|
||||
const retryResult = await buildRetryPayload({chatId, user, session, selectedModel, targetMessage, retryStyle});
|
||||
if (retryResult.error) return {error: retryResult.error, status: 400};
|
||||
|
||||
const existingMessage = formatChatMessage(targetMessage);
|
||||
const retryVariants = [...existingMessage.retryVariants, retryResult.fullReply];
|
||||
persistChatMessageRetryState({
|
||||
chatId,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {exec} from "child_process";
|
||||
import {execFile} from "child_process";
|
||||
|
||||
class ModelServiceError extends Error {
|
||||
constructor(code, message, status = 500) {
|
||||
@@ -29,7 +29,7 @@ export function createModelService(config, overrides = {}) {
|
||||
if (state.timer) clearTimeout(state.timer);
|
||||
state.timer = setTimeout(() => {
|
||||
if (state.activeRequests > 0) return;
|
||||
exec(`ollama stop ${model}`, () => {
|
||||
execFile("ollama", ["stop", model], () => {
|
||||
modelState.delete(model);
|
||||
});
|
||||
}, config.modelUnloadAfterMs);
|
||||
|
||||
@@ -79,13 +79,20 @@ function validateMessage(message, chatIndex, messageIndex, errors) {
|
||||
? message.retry_variants.map((item) => String(item ?? "")).filter(Boolean)
|
||||
: [content].filter(Boolean);
|
||||
return {
|
||||
sourceId: Number.isInteger(Number(message.id)) ? Number(message.id) : null,
|
||||
role: role === "assistant" ? "bot" : role,
|
||||
content,
|
||||
model_name: optionalText(message.model_name || message.modelName),
|
||||
retryVariants,
|
||||
retryActiveIndex: Number.isInteger(Number(message.retryActiveIndex)) ? Number(message.retryActiveIndex) : 0,
|
||||
retryRetriesUsed: Number.isInteger(Number(message.retryRetriesUsed)) ? Number(message.retryRetriesUsed) : 0,
|
||||
retryPromptMessageId: Number.isInteger(Number(message.retryPromptMessageId)) ? Number(message.retryPromptMessageId) : null
|
||||
retryActiveIndex: Number.isInteger(Number(message.retryActiveIndex ?? message.retry_active_index))
|
||||
? Number(message.retryActiveIndex ?? message.retry_active_index)
|
||||
: 0,
|
||||
retryRetriesUsed: Number.isInteger(Number(message.retryRetriesUsed ?? message.retry_retries_used))
|
||||
? Number(message.retryRetriesUsed ?? message.retry_retries_used)
|
||||
: 0,
|
||||
retryPromptMessageId: Number.isInteger(Number(message.retryPromptMessageId ?? message.retry_prompt_message_id))
|
||||
? Number(message.retryPromptMessageId ?? message.retry_prompt_message_id)
|
||||
: null
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user