A hook's calls used to run in whatever session was active at the time. Now `bot_hooks.session_id` pins them to one session, open or closed (null still follows the active one). The hook form picks where it runs (the bot's own chat or one of its workspaces) and a session there, or a new one of its own started beside the active one so the chat on screen stays. A bot's proposed hook gets the session it was proposed in, and a missing session is replaced on the next call. Changing place or session is refused while a run is open; the computer stays only if reachable. Co-Authored-By: Claude Opus 5.5 <[email protected]>
54 lines
1.7 KiB
TypeScript
54 lines
1.7 KiB
TypeScript
import type { HookSessionChoice } from "@/src/types";
|
|
import AppError from "@/src/utils/app-error";
|
|
import grabUserBotSession from "../bot-sessions/grab-user-bot-session";
|
|
import startHookSession from "./start-hook-session";
|
|
|
|
type Params = {
|
|
user_id: number | string;
|
|
bot_id: number | string;
|
|
workspace_id?: number | string | null;
|
|
choice: HookSessionChoice;
|
|
/**
|
|
* The hook's title, which names a new session.
|
|
*/
|
|
title: string;
|
|
/**
|
|
* A chosen session that's gone (or no longer in this conversation)
|
|
* starts a new one instead of being refused: a hook's call, or a bot's
|
|
* proposal confirmed after its session went.
|
|
*/
|
|
missing_starts_new?: boolean;
|
|
};
|
|
|
|
/**
|
|
* The session a hook saved with `choice` lands in: null to follow the
|
|
* conversation's active one, a new one of its own, or one of the user's
|
|
* sessions in that same conversation (refused otherwise).
|
|
*/
|
|
export default async function pickHookSession({
|
|
user_id,
|
|
bot_id,
|
|
workspace_id,
|
|
choice,
|
|
title,
|
|
missing_starts_new,
|
|
}: Params): Promise<number | null> {
|
|
if (choice == "current") return null;
|
|
|
|
if (choice != "new") {
|
|
const session = await grabUserBotSession({ session_id: choice, user_id });
|
|
const here =
|
|
session?.id &&
|
|
Number(session.bot_id) == Number(bot_id) &&
|
|
(Number(session.workspace_id) || null) == (Number(workspace_id) || null);
|
|
|
|
if (here) return Number(session.id);
|
|
|
|
if (!missing_starts_new) {
|
|
throw new AppError(`That session isn't in this conversation.`);
|
|
}
|
|
}
|
|
|
|
return await startHookSession({ user_id, bot_id, workspace_id, title });
|
|
}
|