Lead the rail with the conversation open now
Opening a conversation recorded the visit after the crew was read, so the rail only showed it on top on the next page load. grabConversationProps now records the bot's (and the workspace's) visit before reading, and the bots list pins the open conversation first whatever the visits say.
This commit is contained in:
@@ -90,7 +90,7 @@ Enables SQLite foreign keys (`enableForeignKeys`), `bunextInit()`, `ensureDocker
|
||||
- **Tables:** `bots` (`kind`, `color` `#rrggbb`, `stone`, `role`, `system_prompt`; `hue` is a legacy fallback), `bot_user_faces`, `bot_sessions`, `bot_messages`, `bot_tasks`, `bot_task_steps` (unused yet), `bot_plans` (a plan a bot wrote for work in pieces, its steps and their state), `bot_job_traces` (a run's debug view, 7 days by default). Enums in `src/dict/bot-dict.ts` and `src/dict/bot-plan-dict.ts`.
|
||||
- **Sessions:** one thread per user per bot in its own chat plus one per workspace; split into `bot_sessions` (rollover at the app setting `bot_session_max_messages`, default `AppData.BotSessionMaxMessages`, via `grabBotSessionMaxMessages`; titled and summarised by `name-bot-session.ts` from the session's opening after a reply or a task (a session with no user message, work handed to the bot, is named from the bot's own messages; `grabSessionOpening`; `grabBotMessages` reads the latest; each opening message is cut to `AppData.BotSessionNameMessageChars`, so a pasted brief of tens of thousands of characters costs one naming call, not a second copy of itself), summary carried into the next session only when they're linked by `previous_session_id`, which only a carry-over sets; older implicit links are cleared at boot by `clearImplicitSessionLinks`). New session (`startBotSession`) and a conversation with no active session always start fresh. The rollover carries over like the bot does (`rollOverBotSession` -> `continueBotSession`, falling back to the stored summary; kept for the next message while a job is open in the full session or there's nothing to carry), inside send-message, so the user's message lands under the summary. Helpers in `src/functions/backend/db/bot-sessions/`. Job results land in the job's own session, even after it closes, except a reply that called `continue_in_new_session` (`carryOverBotSession`; the menu's and sessions modal's Continue in new session does the same through `continueBotSession`, the model writing title and summary, refused while a job is open in the session): its summary is kept on the closed session and opens the new one as a bot message (`carried_from_session_id`), the job's `session_id` moves so the answer follows, the new session is titled by the bot then (it has no first exchange to be named from), and the old one can never be reopened (`reopenBotSession` refuses; a thread showing it swaps its composer for a link to the new one). Session lists (rail, sessions modal) reopen a session on click, as before; a carried-over one can't be, so it opens to read (`?session_id=`, `conversationHref`). The page tells them which session it shows (`announceShownSession` -> `useShownSession`): they reload, mark it Current (greyed out; without `?session_id=` the active one is), list it even when it's older than the rail's latest, and the active row then leads back. From half the session length the reply prompt suggests moving on (`longSessionNote`). Earlier sessions never enter the prompt (only the last one's summary): the thread can open them above a session (`useOlderMessages.loadPrevious`, a divider per session, quote and reply as usual), and replies get `search_past_sessions`/`read_past_session` (scoped to that user, bot and place, never the current session) when the conversation has any.
|
||||
- **Coordinator:** exactly one, the super admin's alone, created idempotently by `ensureCoordinatorBot`. Default prompt `src/data/coordinator-system-prompt.ts`, backfilled only when empty.
|
||||
- **Routes:** `/admin/bots` reopens the last visited bot's own chat (cookie `cobble_last_visited_bot_id`, `<bot>` or `<bot>:<workspace>`; the landing uses only the bot) if still reachable, else coordinator (super admin) / latest bot / first workspace / empty. Workspace and conversation pages record the visit per user (`recordWorkspaceVisit`/`workspace_user_visits`, `recordBotVisit`/`bot_user_visits`) and the rail lists both latest visited first (`last_visited_at`, `orderByLastVisit`; a workspace nobody visited yet ranks by its creation, so a new one leads); it never decides the landing. Settings and the rail pages get the cookie's conversation, workspace included, as root prop `last_conversation` (`grabLastConversation`, access-checked, only on those paths: `isPagesRailPath`), shown above their rail list as the way back (`RailLastConversation`). `/admin/bots/[bot_id]` conversation (header, thread and composer at 760px, no right panel; `?session_id=` opens another of the user's sessions there: a closed one is reopened, except a carried-over one, which shows with `SessionMovedBar` instead of the composer, pointing where the conversation went on); `/admin/bots/[bot_id]/models`; `/admin/workspaces/[id]` dashboard; `/admin/workspaces/[id]/bots/[bot_id]` same conversation scoped to the workspace.
|
||||
- **Routes:** `/admin/bots` reopens the last visited bot's own chat (cookie `cobble_last_visited_bot_id`, `<bot>` or `<bot>:<workspace>`; the landing uses only the bot) if still reachable, else coordinator (super admin) / latest bot / first workspace / empty. Workspace and conversation pages record the visit per user (`recordWorkspaceVisit`/`workspace_user_visits`, `recordBotVisit`/`bot_user_visits`) and the rail lists both latest visited first (`last_visited_at`, `orderByLastVisit`; recorded before the crew is read, so the conversation opened is already on top in that render, and its bot leads whatever the visits say (`pinned_id`); a workspace nobody visited yet ranks by its creation, so a new one leads); it never decides the landing. Settings and the rail pages get the cookie's conversation, workspace included, as root prop `last_conversation` (`grabLastConversation`, access-checked, only on those paths: `isPagesRailPath`), shown above their rail list as the way back (`RailLastConversation`). `/admin/bots/[bot_id]` conversation (header, thread and composer at 760px, no right panel; `?session_id=` opens another of the user's sessions there: a closed one is reopened, except a carried-over one, which shows with `SessionMovedBar` instead of the composer, pointing where the conversation went on); `/admin/bots/[bot_id]/models`; `/admin/workspaces/[id]` dashboard; `/admin/workspaces/[id]/bots/[bot_id]` same conversation scoped to the workspace.
|
||||
- **No model:** while the bot has no text model there (`bot_text_model` null, kept live by the models modal), the thread shows `NoModelNotice` (centred when empty): super admins get Connect a provider / Guided setup, others are told the super admin sets it up.
|
||||
- **Conversation loading:** pages render everything server-side (`grabConversationProps`) **except the history**, which the client loads (`api/admin/bots/conversation-history`, merged with what's already shown, refetched on bfcache restore) and pages older messages on scroll. Rail lists go through `railBots`/`railWorkspaces` (no prompts/memory, previews cut). Every return to the page (window focus, a hidden tab visible, back online: `use-refocus`, `AppData.RefocusGapMs`) reads the crew again (`use-live-crew-refresh` -> `api/admin/bots/live-crew` -> `cobble:live-crew-refreshed`, taken by `useLiveBots`/`useLiveWorkspaces`) and the session's latest messages plus its bot's face, task and the step its reply is on (`session-messages`, `use-reply-catch-up`; the history's own `step` seeds that row on load, `use-reply-progress`, and a row only ever moves forward in `bots_queue_updates` ids), and the page polls the same while a reply is due (read with the live face, never the page's): pushes missed while the phone slept left threads thinking and faces stale until a reload. On a phone, pulling the thread up past its bottom lifts the newest message off the edge, opening just enough room for a lone refresh icon (a ghost button, no label; no deeper, however far the drag goes), and letting go only reloads once that full room has been held for half a second, which lights the icon (`AppData.PullToRefreshHoldMs`: an exhausted thread reaches the limit by accident too easily) and then turns it into its spinner for at least a second (`AppData.PullToRefreshMinLoadingMs`) while the conversation and the rail are read again (`use-pull-to-refresh` writes that room straight onto the content as the finger moves, never through a render, and owns the gesture until release; `PullToRefreshIndicator`; dragged back down before the release, the icon hides at the travel that showed it).
|
||||
- **Sending:** `api/admin/bots/send-message.ts` -> `enqueueBotReply` -> `wakeBotsQueue`. **Never import `src/bots-queue/index.ts` from a page or API route** (pulls the bunext runtime into page bundles). Optimistic bubble in `pending_messages` until the saved message replaces it. A message is capped at the app setting `bot_message_max_chars` (Settings > General, default `AppData.BotMessageMaxChars`, counted on the trimmed markdown, up to `BotMessageMaxCharsMax`), checked by the composer before it sends (the reason shows under the input) and again by the route (`messageTooLongMessage` says how far over it is and to attach the text as a file). Once stored, a message is never cut: it goes into every turn whole.
|
||||
|
||||
@@ -3,6 +3,8 @@ import type { BotTextModel, PagePropsType } from "@/src/types";
|
||||
import railBots from "@/src/utils/rail-bots";
|
||||
import railWorkspaces from "@/src/utils/rail-workspaces";
|
||||
import grabBots from "../bots/grab-bots";
|
||||
import recordBotVisit from "../bots/record-bot-visit";
|
||||
import recordWorkspaceVisit from "../workspaces/record-workspace-visit";
|
||||
import grabBotExtensionsOverview from "../extensions/grab-bot-extensions-overview";
|
||||
import grabConversationFiles from "../bot-files/grab-conversation-files";
|
||||
import grabBotComputers from "../computers/grab-bot-computers";
|
||||
@@ -36,7 +38,8 @@ type Params = {
|
||||
* the models modal: the install-wide catalog (`api/admin/harness/catalog`)
|
||||
* and the conversation's small setting
|
||||
* (`api/admin/bots/harness-overview`). They are most of a page's weight
|
||||
* and only that modal uses them.
|
||||
* and only that modal uses them. Opening a conversation records the visit
|
||||
* (`recordBotVisit`, and the workspace's), before the rail is read.
|
||||
* Null when the user can't chat with the bot there.
|
||||
*/
|
||||
export default async function grabConversationProps({
|
||||
@@ -52,6 +55,17 @@ export default async function grabConversationProps({
|
||||
|
||||
const scope = conversation.workspace_id;
|
||||
|
||||
/**
|
||||
* Noted before the rail is read, so the conversation just opened is
|
||||
* already the latest in it: the lists order by each user's last visit,
|
||||
* and reading them first left the open bot where it was until the
|
||||
* next page load.
|
||||
*/
|
||||
await Promise.all([
|
||||
recordBotVisit({ user_id, bot_id: conversation.bot.id }),
|
||||
scope ? recordWorkspaceVisit({ user_id, workspace_id: scope }) : null,
|
||||
]);
|
||||
|
||||
const [bots, workspaces, answer_ask] = await Promise.all([
|
||||
grabBots({ user_id, workspace_id: scope }),
|
||||
grabUserWorkspaces({ user_id }),
|
||||
|
||||
@@ -32,12 +32,12 @@ type Props = {
|
||||
|
||||
/**
|
||||
* A section of bots in the rail: the latest `AppData.RailRecentBots` visited
|
||||
* (and any that want attention), then "See all bots" for the rest,
|
||||
* which lists them all in a modal.
|
||||
* (the conversation open now first, and any that want attention), then
|
||||
* "See all bots" for the rest, which lists them all in a modal.
|
||||
*/
|
||||
export default function RailBotsSection({ title, workers: listed, selected_id, workspace_id, hrefFor, empty, action }: Props) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const workers = orderByLastVisit({ items: listed });
|
||||
const workers = orderByLastVisit({ items: listed, pinned_id: selected_id });
|
||||
const { shown, hidden } = pickRailBots({ workers, selected_id, limit: AppData.RailRecentBots });
|
||||
|
||||
return (
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabConversationProps from "@/src/functions/backend/db/bot-sessions/grab-conversation-props";
|
||||
import recordBotVisit from "@/src/functions/backend/db/bots/record-bot-visit";
|
||||
import withLastVisitedBotCookies from "@/src/functions/server/with-last-visited-bot-cookies";
|
||||
import type { PagePropsType, PageQueryObject } from "@/src/types";
|
||||
import type { BunextPageServerFn } from "@moduletrace/bunext/types";
|
||||
@@ -36,8 +35,6 @@ export const server: BunextPageServerFn<PagePropsType> = async (params) => {
|
||||
|
||||
const bot_id = props.bot.id;
|
||||
|
||||
await recordBotVisit({ user_id: user.id, bot_id });
|
||||
|
||||
return {
|
||||
props,
|
||||
res_transform(res) {
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import grabConversationProps from "@/src/functions/backend/db/bot-sessions/grab-conversation-props";
|
||||
import recordBotVisit from "@/src/functions/backend/db/bots/record-bot-visit";
|
||||
import recordWorkspaceVisit from "@/src/functions/backend/db/workspaces/record-workspace-visit";
|
||||
import withLastVisitedBotCookies from "@/src/functions/server/with-last-visited-bot-cookies";
|
||||
import type { PagePropsType, PageQueryObject } from "@/src/types";
|
||||
import canManageCrew from "@/src/utils/rbac/can-manage-crew";
|
||||
@@ -42,12 +40,6 @@ export const server: BunextPageServerFn<PagePropsType> = async (params) => {
|
||||
const bot_id = props.bot.id;
|
||||
const workspace_id = props.workspace?.id || null;
|
||||
|
||||
await recordBotVisit({ user_id: user.id, bot_id });
|
||||
|
||||
if (props.workspace?.id) {
|
||||
await recordWorkspaceVisit({ user_id: user.id, workspace_id: props.workspace.id });
|
||||
}
|
||||
|
||||
return {
|
||||
props,
|
||||
res_transform(res) {
|
||||
|
||||
@@ -35,6 +35,29 @@ describe("orderByLastVisit", () => {
|
||||
expect(items.map((item) => item.id)).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
test("the pinned item leads even when another was visited later", () => {
|
||||
const items = [
|
||||
{ id: 1, last_visited_at: 500 },
|
||||
{ id: 2, last_visited_at: 900 },
|
||||
{ id: 3 },
|
||||
{ id: 4, last_visited_at: 400 },
|
||||
];
|
||||
|
||||
expect(orderByLastVisit({ items, pinned_id: 1 }).map((item) => item.id)).toEqual([1, 2, 4, 3]);
|
||||
expect(orderByLastVisit({ items, pinned_id: 3 }).map((item) => item.id)).toEqual([3, 2, 1, 4]);
|
||||
});
|
||||
|
||||
test("the pin matches the id either way and changes nothing when absent", () => {
|
||||
const items = [
|
||||
{ id: 1, last_visited_at: 100 },
|
||||
{ id: 2, last_visited_at: 200 },
|
||||
];
|
||||
|
||||
expect(orderByLastVisit({ items, pinned_id: "2" }).map((item) => item.id)).toEqual([2, 1]);
|
||||
expect(orderByLastVisit({ items, pinned_id: 9 }).map((item) => item.id)).toEqual([2, 1]);
|
||||
expect(orderByLastVisit({ items, pinned_id: null }).map((item) => item.id)).toEqual([2, 1]);
|
||||
});
|
||||
|
||||
test("a new unvisited item leads when ranked by creation", () => {
|
||||
const items = [
|
||||
{ id: 1, created_at: 10, last_visited_at: 500 },
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
type Item = { last_visited_at?: number | null; created_at?: number | string | null };
|
||||
type Item = {
|
||||
id?: number | string;
|
||||
last_visited_at?: number | null;
|
||||
created_at?: number | string | null;
|
||||
};
|
||||
|
||||
type Params<T> = {
|
||||
items: T[];
|
||||
@@ -8,18 +12,29 @@ type Params<T> = {
|
||||
* visited one, out of the rail's shown slice.
|
||||
*/
|
||||
unvisited_by_created?: boolean;
|
||||
/**
|
||||
* The conversation open now: it leads whatever the visits say, so it
|
||||
* is already on top as the page arrives and a refresh from another
|
||||
* device can't push it down.
|
||||
*/
|
||||
pinned_id?: number | string | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* A rail list (workspaces, bots): what the user has visited first, latest
|
||||
* first, then the rest in their order. With `unvisited_by_created`, an
|
||||
* unvisited item ranks by its `created_at` among the visited ones.
|
||||
* unvisited item ranks by its `created_at` among the visited ones; with
|
||||
* `pinned_id`, that item leads them all.
|
||||
*/
|
||||
export default function orderByLastVisit<T extends Item>({ items, unvisited_by_created }: Params<T>): T[] {
|
||||
export default function orderByLastVisit<T extends Item>({ items, unvisited_by_created, pinned_id }: Params<T>): T[] {
|
||||
const rank = (item: T) =>
|
||||
Number(item.last_visited_at || (unvisited_by_created ? item.created_at : 0) || 0);
|
||||
|
||||
const ranked = items.filter((item) => rank(item)).sort((a, b) => rank(b) - rank(a));
|
||||
const pinned =
|
||||
pinned_id == null ? undefined : items.find((item) => item.id == pinned_id);
|
||||
const rest = pinned ? items.filter((item) => item !== pinned) : items;
|
||||
const ranked = rest.filter((item) => rank(item)).sort((a, b) => rank(b) - rank(a));
|
||||
const ordered = [...ranked, ...rest.filter((item) => !rank(item))];
|
||||
|
||||
return [...ranked, ...items.filter((item) => !rank(item))];
|
||||
return pinned ? [pinned, ...ordered] : ordered;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user