Show a newly created bot in the rail at once

The rail was built from page props and only knew bots it already had, so a
bot the coordinator created did not appear until a reload.

create_bot and add_bot_to_workspace now push server:bot-created
(BotCreatedData) to everyone who sees the bot: its rail row, the
workspaces it is in, and whether it belongs in that user's own chat list.
useLiveBots inserts it, after the pinned coordinator outside a workspace
and only when the open workspace is one it joined. liveRelay pushes created
bots too, so a bot added by another process still shows.
This commit is contained in:
2026-10-19 12:17:56 +01:00
parent c1cb313bd0
commit fccaa6e280
9 changed files with 180 additions and 3 deletions
+5 -1
View File
@@ -3,6 +3,7 @@ import { AppData } from "@/src/data/app-data";
import { logError } from "@/src/functions/backend/logger";
import grabConnectedUserIds from "@/src/websocket/(functions)/grab-connected-user-ids";
import relayChangedBots from "./relay-changed-bots";
import relayCreatedBots from "./relay-created-bots";
import relayNewBotMessages from "./relay-new-bot-messages";
import relayNewJobUpdates from "./relay-new-job-updates";
import type { LiveRelayCursor } from "./types";
@@ -50,7 +51,10 @@ export default async function liveRelay() {
* Overlap one interval, so a change written in the same
* millisecond as the last look isn't missed.
*/
await relayChangedBots({ since: cursor.bots_at - AppData.LiveRelayPollMs, users });
const bots_since = cursor.bots_at - AppData.LiveRelayPollMs;
await relayCreatedBots({ since: bots_since, users });
await relayChangedBots({ since: bots_since, users });
cursor.bots_at = now;
} catch (error) {
await logError({ title: `Live relay failed`, error }).catch(() => {});
+33
View File
@@ -0,0 +1,33 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_BOTS } from "@/db/types/db";
import pushBotCreated from "@/src/bots-queue/push-bot-created";
type Params = {
/**
* Unix ms: bots created since then are pushed.
*/
since: number;
users: Set<string>;
};
/**
* Pushes `server:bot-created` for every bot created since `since` to the
* connected users who see it (`pushBotCreated`), so a bot another process
* added still shows in the rail. Pushes are idempotent, so overlapping
* windows only repeat them.
*/
export default async function relayCreatedBots({ since, users }: Params) {
if (!users.size) return;
const bots =
(
await BunSQLite.sql<BUN_SQLITE_COBBLE_BOTS>({
sql: `SELECT * FROM bots WHERE created_at > ?`,
values: [since],
})
).payload || [];
for (const bot of bots) {
await pushBotCreated({ bot, users });
}
}