Update the plan bar from the card, and make Plan mode unmistakable
The card that moves a plan now announces the new plan on the page, so the header bar and every card of it change without waiting for a socket push that a route may never land. The composer's Plan button is filled in the primary colour while the mode is on, and the notice above the input is gone: the header tag and the button already say it.
This commit is contained in:
@@ -4,7 +4,11 @@ import type { ComposerExtensionsCopy } from "../(partials)/composer-tools-modal"
|
||||
/**
|
||||
* The composer's tools button: MCP servers, tools and skills.
|
||||
*/
|
||||
export function composerToolsCopy({ bot_name }: { bot_name: string }): ComposerExtensionsCopy {
|
||||
export function composerToolsCopy({
|
||||
bot_name,
|
||||
}: {
|
||||
bot_name: string;
|
||||
}): ComposerExtensionsCopy {
|
||||
return {
|
||||
id: "composer-add-extension",
|
||||
Icon: Wrench,
|
||||
@@ -22,7 +26,11 @@ export function composerToolsCopy({ bot_name }: { bot_name: string }): ComposerE
|
||||
/**
|
||||
* The composer's knowledgebases button, beside the tools.
|
||||
*/
|
||||
export function composerKnowledgebasesCopy({ bot_name }: { bot_name: string }): ComposerExtensionsCopy {
|
||||
export function composerKnowledgebasesCopy({
|
||||
bot_name,
|
||||
}: {
|
||||
bot_name: string;
|
||||
}): ComposerExtensionsCopy {
|
||||
return {
|
||||
id: "composer-add-knowledgebase",
|
||||
Icon: Library,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import type { BotPlanChangedData, BotPlanView } from "@/src/types";
|
||||
import useServerEvent from "@/src/hooks/use-server-event";
|
||||
import { BotPlanChangedEvent } from "@/src/utils/bot-plan-changed-event";
|
||||
import fetchBotPlan from "../(functions)/fetch-bot-plan";
|
||||
|
||||
type Params = {
|
||||
@@ -63,6 +64,22 @@ export default function useBotPlan({ plan_id }: Params) {
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (event: Event) => {
|
||||
const plan = (event as CustomEvent<{ plan?: BotPlanView }>).detail?.plan;
|
||||
|
||||
if (!plan || Number(plan.id) != plan_id) return;
|
||||
|
||||
setViewState(plan);
|
||||
setError(undefined);
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
window.addEventListener(BotPlanChangedEvent, listener);
|
||||
|
||||
return () => window.removeEventListener(BotPlanChangedEvent, listener);
|
||||
}, [plan_id]);
|
||||
|
||||
/**
|
||||
* The card's own moves replace what was read, so it shows the change
|
||||
* without asking the app again.
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { useEffect, useRef } from "react";
|
||||
import type { BotPlanChangedData, BotPlanView } from "@/src/types";
|
||||
import useServerEvent from "@/src/hooks/use-server-event";
|
||||
import { BotPlanChangedEvent } from "@/src/utils/bot-plan-changed-event";
|
||||
import fetchBotPlan from "../(functions)/fetch-bot-plan";
|
||||
|
||||
type Params = {
|
||||
@@ -13,10 +15,14 @@ type Params = {
|
||||
/**
|
||||
* The plan open in the conversation, for the thread's plan bar: seeded by
|
||||
* the history's `plan`, then kept current from the app's pushes (written, a
|
||||
* step done, approved, paused, ended). The bar hides a plan that is no
|
||||
* step done, approved, paused, ended) and from the card that moved it,
|
||||
* which announces the change at once. The bar hides a plan that is no
|
||||
* longer open, so a finished one is read rather than pushed away.
|
||||
*/
|
||||
export default function useSessionPlan({ session_id, on_change }: Params) {
|
||||
const change = useRef(on_change);
|
||||
change.current = on_change;
|
||||
|
||||
useServerEvent({
|
||||
event: "server:bot-plan",
|
||||
onData: (data: BotPlanChangedData) => {
|
||||
@@ -24,9 +30,23 @@ export default function useSessionPlan({ session_id, on_change }: Params) {
|
||||
|
||||
void fetchBotPlan({ plan_id: Number(data.plan_id) })
|
||||
.then((res) => {
|
||||
if (res.success && res.singleRes) on_change(res.singleRes);
|
||||
if (res.success && res.singleRes) change.current(res.singleRes);
|
||||
})
|
||||
.catch(() => null);
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (event: Event) => {
|
||||
const plan = (event as CustomEvent<{ plan?: BotPlanView }>).detail?.plan;
|
||||
|
||||
if (!plan || !session_id || Number(plan.session_id) != Number(session_id)) return;
|
||||
|
||||
change.current(plan);
|
||||
};
|
||||
|
||||
window.addEventListener(BotPlanChangedEvent, listener);
|
||||
|
||||
return () => window.removeEventListener(BotPlanChangedEvent, listener);
|
||||
}, [session_id]);
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import setBotPlanState from "../(functions)/set-bot-plan-state";
|
||||
import useBotPlan from "../(hooks)/use-bot-plan";
|
||||
import botPlanStatusLine from "../(utils)/bot-plan-status-line";
|
||||
import announcePlanModeOff from "@/src/utils/plan-mode-event";
|
||||
import announceBotPlanChanged from "@/src/utils/bot-plan-changed-event";
|
||||
import BotPlanEditModal from "./bot-plan-edit-modal";
|
||||
import BotPlanStepRow from "./bot-plan-step-row";
|
||||
|
||||
@@ -68,6 +69,7 @@ export default function BotPlanCard({ plan_id }: Props) {
|
||||
if (status == "active") announcePlanModeOff();
|
||||
|
||||
setView(res.singleRes);
|
||||
announceBotPlanChanged({ plan: res.singleRes });
|
||||
};
|
||||
|
||||
const can_change = view.status == "planned" || view.status == "active" || view.status == "paused";
|
||||
|
||||
@@ -9,24 +9,24 @@ type Props = {
|
||||
|
||||
/**
|
||||
* Beside the composer's tools: turns Plan mode on for the next message, so
|
||||
* the bot plans and reads and changes nothing. Its state is the button's
|
||||
* own, filled while it is on (the Tab key turns it on and off anywhere on
|
||||
* the page).
|
||||
* the bot plans and reads and changes nothing. Filled in the primary colour
|
||||
* while it is on, so the mode is unmistakable (the Tab key turns it on and
|
||||
* off anywhere on the page).
|
||||
*/
|
||||
export default function ComposerPlanButton({ plan_mode, onToggle }: Props) {
|
||||
return (
|
||||
<Button
|
||||
title={plan_mode ? "Plan mode is on: the bot plans and reads, and changes nothing (Tab turns it off)" : "Plan mode: the bot plans and reads, and changes nothing (Tab turns it on)"}
|
||||
type="button"
|
||||
variant="ghost"
|
||||
color="gray"
|
||||
variant={plan_mode ? "normal" : "ghost"}
|
||||
color={plan_mode ? "primary" : "gray"}
|
||||
size="small"
|
||||
aria-pressed={plan_mode}
|
||||
beforeIcon={<ListChecks size={12} aria-hidden />}
|
||||
onClick={onToggle}
|
||||
className={twMerge(
|
||||
"shrink-0 gap-1! px-2! py-0.5! text-[12px] leading-4 text-ink-secondary",
|
||||
plan_mode && "bg-ink/5 text-ink dark:bg-ink/5 dark:text-ink",
|
||||
"shrink-0 gap-1! px-2! py-0.5! text-[12px] leading-4",
|
||||
!plan_mode && "text-ink-secondary",
|
||||
)}
|
||||
>
|
||||
Plan
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import P from "@moduletrace/twui/layout/P";
|
||||
|
||||
/**
|
||||
* Above the composer's input while Plan mode is on: what the next message
|
||||
* does, and how to turn it off, since the mode changes what the bot may
|
||||
* touch and must never be a hidden state.
|
||||
*/
|
||||
export default function ComposerPlanNotice() {
|
||||
return (
|
||||
<P className="w-full max-w-container mx-auto px-3 md:px-6 pt-3 text-xs text-ink-secondary">
|
||||
Plan mode is on: the bot plans and reads, and changes nothing. Tab turns it off.
|
||||
</P>
|
||||
);
|
||||
}
|
||||
@@ -34,7 +34,6 @@ import ComposerAttachments from "./composer-attachments";
|
||||
import ComposerClearButton from "./composer-clear-button";
|
||||
import ComposerEditor from "./composer-editor";
|
||||
import ComposerMentionList from "./composer-mention-list";
|
||||
import ComposerPlanNotice from "./composer-plan-notice";
|
||||
import ComposerReplyPreview from "./composer-reply-preview";
|
||||
import ComposerSendButton from "./composer-send-button";
|
||||
import ComposerTools from "./composer-tools";
|
||||
@@ -433,7 +432,6 @@ export default function Composer({
|
||||
/>
|
||||
) : null}
|
||||
{input.menu ? <ComposerMentionList menu={input.menu} /> : null}
|
||||
{plan_mode ? <ComposerPlanNotice /> : null}
|
||||
<Row
|
||||
className={`w-full max-w-container mx-auto min-h-[88px] ${reply_to ? "pt-3 pb-7" : "py-7"} gap-3! md:gap-4! flex-nowrap items-end px-3 md:px-6 cursor-text`}
|
||||
onMouseDown={(e) => {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import type { BotPlanView } from "@/src/types";
|
||||
import announceBotPlanChanged, { BotPlanChangedEvent } from "./bot-plan-changed-event";
|
||||
|
||||
const plan = { id: 3, session_id: 133, title: "Plan", status: "cancelled" } as BotPlanView;
|
||||
|
||||
describe("announceBotPlanChanged", () => {
|
||||
test("tells every listener on the page about the plan", () => {
|
||||
const target = new EventTarget();
|
||||
const heard: BotPlanView[] = [];
|
||||
|
||||
target.addEventListener(BotPlanChangedEvent, (event) => {
|
||||
heard.push((event as CustomEvent<{ plan: BotPlanView }>).detail.plan);
|
||||
});
|
||||
|
||||
announceBotPlanChanged({ plan, target });
|
||||
|
||||
expect(heard).toEqual([plan]);
|
||||
});
|
||||
|
||||
test("says nothing where there is no page", () => {
|
||||
expect(() => announceBotPlanChanged({ plan, target: null })).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { BotPlanView } from "@/src/types";
|
||||
|
||||
export const BotPlanChangedEvent = "cobble:bot-plan-changed";
|
||||
|
||||
type Params = {
|
||||
plan: BotPlanView;
|
||||
target?: EventTarget | null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Tells the page that a plan changed, from the card that moved it. The app
|
||||
* also pushes `server:bot-plan`, but a push that never lands (a socket
|
||||
* between reconnects, a route handled away from the sockets' module
|
||||
* instance) must not leave the plan bar, and the thread's other cards of
|
||||
* the same plan, showing the status it had before the click.
|
||||
*/
|
||||
export default function announceBotPlanChanged({
|
||||
plan,
|
||||
target = typeof window == "undefined" ? null : window,
|
||||
}: Params) {
|
||||
target?.dispatchEvent(new CustomEvent(BotPlanChangedEvent, { detail: { plan } }));
|
||||
}
|
||||
Reference in New Issue
Block a user