Carry conversations into new sessions with a visible summary
- Bots get continue_in_new_session: the bot writes a title and summary, the session closes for good (read-only) and the new one opens with the summary as a "Carried over from the last session" message that links back to the old session. - Continue in new session (conversation menu, sessions modal) and the automatic rollover at the session length do the same; New session now starts a fresh, unlinked session. - Session length is an app setting in Settings > General. - ?session_id= opens a session; a carried-over one shows a bar linking to where the conversation went on instead of the composer. - The thread can open the previous session above the current one (shown only, never in the prompt), and replies get search_past_sessions and read_past_session. - Sessions are named from their opening messages, not their latest, and a carried summary is labelled as the earlier session's. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import type { BUN_SQLITE_COBBLE_BOT_MESSAGES } from "@/db/types/db";
|
||||
import pushBotNotice from "@/src/bots-queue/push-bot-notice";
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import continueBotSession from "@/src/functions/backend/db/bot-sessions/continue-bot-session";
|
||||
import grabUserConversation from "@/src/functions/backend/db/bot-sessions/grab-user-conversation";
|
||||
import { BotConversationRequestSchema } from "@/src/schemas/bot-schema";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import isReadOnlyUser from "@/src/utils/rbac/is-read-only-user";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type {
|
||||
APIResponseObject,
|
||||
BunextAPIRouteHandler,
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Continue in new session: carries one of the session user's
|
||||
* conversations into a new session with a summary of the active one, as
|
||||
* the bot does when asked (`continueBotSession`). The active one is closed
|
||||
* for good. Unlike `new-session`, which starts fresh.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<
|
||||
APIResponseObject<BUN_SQLITE_COBBLE_BOT_MESSAGES>
|
||||
> = async ({ req, body }) => {
|
||||
try {
|
||||
if (req.method != "POST") {
|
||||
throw new AppError(`Method not allowed`);
|
||||
}
|
||||
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
|
||||
if (!user?.logged_in_status || !user.id) {
|
||||
return { success: false, msg: `Unauthorized`, logoutUser: true };
|
||||
}
|
||||
|
||||
if (isReadOnlyUser({ user_types: user_types || [] })) {
|
||||
throw new AppError(`This user can only read records.`);
|
||||
}
|
||||
|
||||
const { bot_id, workspace_id } = parseWithSchema({
|
||||
schema: BotConversationRequestSchema,
|
||||
data: body,
|
||||
});
|
||||
|
||||
const conversation = await grabUserConversation({ user_id: user.id, bot_id, workspace_id });
|
||||
|
||||
const message = await continueBotSession({
|
||||
user_id: user.id,
|
||||
bot_id: Number(conversation.bot.id),
|
||||
workspace_id: conversation.workspace_id ? Number(conversation.workspace_id) : null,
|
||||
});
|
||||
|
||||
await pushBotNotice({ bot_id: Number(conversation.bot.id), message, quiet_push: true });
|
||||
|
||||
return { success: true, singleRes: message };
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabConversationHistory from "@/src/functions/backend/db/bot-sessions/grab-conversation-history";
|
||||
import { BotConversationRequestSchema } from "@/src/schemas/bot-schema";
|
||||
import { ConversationHistoryRequestSchema } from "@/src/schemas/bot-schema";
|
||||
import type { ConversationHistoryPayload } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
@@ -12,7 +12,8 @@ import type {
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* A conversation's history (`grabConversationHistory`: its active session,
|
||||
* A conversation's history (`grabConversationHistory`: its active session
|
||||
* or the one `session_id` names,
|
||||
* latest messages and the bot's active task there), which the
|
||||
* conversation pages load once they are on screen instead of rendering it
|
||||
* on the server, so a page restored by the back button never shows an
|
||||
@@ -34,8 +35,8 @@ export const handler: BunextAPIRouteHandler<
|
||||
return { success: false, msg: `Unauthorized`, logoutUser: true };
|
||||
}
|
||||
|
||||
const { bot_id, workspace_id } = parseWithSchema({
|
||||
schema: BotConversationRequestSchema,
|
||||
const { bot_id, workspace_id, session_id } = parseWithSchema({
|
||||
schema: ConversationHistoryRequestSchema,
|
||||
data: body,
|
||||
});
|
||||
|
||||
@@ -43,6 +44,7 @@ export const handler: BunextAPIRouteHandler<
|
||||
user_id: Number(user.id),
|
||||
bot_id,
|
||||
workspace_id,
|
||||
session_id,
|
||||
});
|
||||
|
||||
if (!history) {
|
||||
|
||||
@@ -13,8 +13,9 @@ import type {
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Closes the active session of one of the session user's conversations and
|
||||
* starts the next one, which carries on from it.
|
||||
* New session: closes the active session of one of the session user's
|
||||
* conversations and starts a fresh one, linked to nothing and carrying no
|
||||
* summary. Carrying the conversation on is `continue-session`.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<
|
||||
APIResponseObject<BUN_SQLITE_COBBLE_BOT_SESSIONS>
|
||||
|
||||
@@ -13,9 +13,10 @@ import type {
|
||||
} from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* The messages before `before_id` in one of the session user's own
|
||||
* sessions, `AppData.ConversationOlderMessages` at a time, for the thread
|
||||
* scrolled to its top.
|
||||
* The messages before `before_id` (or the latest) in one of the session
|
||||
* user's own sessions, `AppData.ConversationOlderMessages` at a time, for
|
||||
* the thread scrolled to its top or opening the previous session above
|
||||
* it. Reading an old session never puts it in the bot's prompt.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<
|
||||
APIResponseObject<OlderBotMessagesData>
|
||||
@@ -52,7 +53,17 @@ export const handler: BunextAPIRouteHandler<
|
||||
|
||||
return {
|
||||
success: true,
|
||||
singleRes: { messages: has_more ? messages.slice(1) : messages, has_more },
|
||||
singleRes: {
|
||||
messages: has_more ? messages.slice(1) : messages,
|
||||
has_more,
|
||||
session: {
|
||||
id: Number(session.id),
|
||||
title: session.title || null,
|
||||
previous_session_id: session.previous_session_id
|
||||
? Number(session.previous_session_id)
|
||||
: null,
|
||||
},
|
||||
},
|
||||
};
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import grabSuperAdminUser from "@/src/functions/backend/backups/grab-super-admin-user";
|
||||
import grabSessionLengthOverview from "@/src/functions/backend/db/app-settings/grab-bot-session-max-messages";
|
||||
import saveAppSetting from "@/src/functions/backend/db/app-settings/save-app-setting";
|
||||
import logEvent, { grabRequestIP } from "@/src/functions/backend/logger";
|
||||
import { BotSessionMaxMessagesSettingKey } from "@/src/dict/bot-dict";
|
||||
import { SaveSessionLengthRequestSchema } from "@/src/schemas/bot-schema";
|
||||
import type { SessionLengthOverview } from "@/src/types";
|
||||
import AppError from "@/src/utils/app-error";
|
||||
import parseWithSchema from "@/src/utils/parse-with-schema";
|
||||
import safeErrorMessage from "@/src/utils/safe-error-message";
|
||||
import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types";
|
||||
|
||||
/**
|
||||
* Super admin only: how many messages a session takes before the user's
|
||||
* next message starts a new one. Zero puts the code's default back.
|
||||
*/
|
||||
export const handler: BunextAPIRouteHandler<APIResponseObject<SessionLengthOverview>> = async ({ req, body }) => {
|
||||
try {
|
||||
if (req.method != "POST") throw new AppError(`Method not allowed`);
|
||||
|
||||
const user = await grabSuperAdminUser({ req });
|
||||
|
||||
if (!user?.id) throw new AppError(`Only a super admin can change how long sessions run.`);
|
||||
|
||||
const { max_messages } = parseWithSchema({ schema: SaveSessionLengthRequestSchema, data: body });
|
||||
|
||||
await saveAppSetting({
|
||||
key: BotSessionMaxMessagesSettingKey,
|
||||
value: max_messages > 0 ? String(max_messages) : null,
|
||||
});
|
||||
|
||||
await logEvent({
|
||||
title: "Session length changed",
|
||||
category: "system",
|
||||
user_id: user.id,
|
||||
plain_text: `User #${user.id} set sessions to take ${max_messages || "the default number of"} messages.`,
|
||||
metadata: { max_messages },
|
||||
ip_address: grabRequestIP(req),
|
||||
});
|
||||
|
||||
return { success: true, singleRes: await grabSessionLengthOverview() };
|
||||
} catch (error: unknown) {
|
||||
return { success: false, msg: safeErrorMessage({ error }) };
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user