Files
cobble/src/pages/admin/bots/[bot_id]/(hooks)/use-task-progress.ts
T
tbenandClaude Opus 5.5 e45d54d30b Share one crew across the install, keep user memory per user
Bots, workspaces and computers now belong to the install. Users reach
workers only through bot_users (assigned by a super admin or admin) and
workspace_users; the coordinator and main computer are one per install,
and only the super admin sees the coordinator. Conversations are per
user (bot_sessions.user_id), jobs run for the session's user, and live
updates go only to who should see them.

A bot's core memory is now general knowledge shared by every user it
works for; what it learns about a user lives in bot_user_memories.

One-off scripts move existing installs over: move-bot-memory-to-owners
(run first), then share-crew.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 11:37:14 +01:00

65 lines
2.0 KiB
TypeScript

import { useState } from "react";
import useServerEvent from "@/src/hooks/use-server-event";
import type { BotJobUpdateData, BotTaskWithSteps, BotUpdatedData } from "@/src/types";
type Params = {
bot_id: number;
/**
* The open session: steps of jobs from other sessions are left out.
*/
session_id?: number | null;
/**
* The bot's active task when the page loaded.
*/
task: BotTaskWithSteps | null;
};
type Progress = {
task_id: number | null;
text: string | null;
};
function fromTask({ task }: { task: BotTaskWithSteps | null }): Progress {
return {
task_id: task?.id || null,
text: task?.updates[task.updates.length - 1]?.text || null,
};
}
/**
* The open session's active task and its latest step (a
* `bots_queue_updates` row's text), kept current from
* `server:bot-job-update`. Starts over when `server:bot-updated` brings a
* different task, or none.
*/
export default function useTaskProgress({ bot_id, session_id, task }: Params) {
const [progress, setProgress] = useState<Progress>(() => fromTask({ task }));
useServerEvent({
event: "server:bot-job-update",
onData: (update: BotJobUpdateData) => {
if (update.bot_id != bot_id || !update.task_id || !update.text) return;
if (session_id && update.session_id && update.session_id != session_id) return;
setProgress({ task_id: update.task_id, text: update.text });
},
});
useServerEvent({
event: "server:bot-updated",
onData: (data: BotUpdatedData) => {
if (data.id != bot_id) return;
/**
* The user's active task with the bot may be in another of
* their conversations with it.
*/
const task = data.task && (!session_id || data.task.session_id == session_id) ? data.task : null;
setProgress((prev) => ((task?.id || null) == prev.task_id ? prev : fromTask({ task })));
},
});
return progress;
}