Files
DeepTutor/web/hooks/useWorkspaceChatActions.ts
jiangbianshu 1d268987fc fix(reading): send the action the reading composer displays
The reading composer hides Course Study from its action menu and shows
any action it does not offer as Chat, but left the stored capability in
chat state untouched. Turns are built from chat state, so after a course
hand-off opened a video or document in Reading, every message was still
sent as `course_study`. That loop only recommends next steps and never
teaches, so questions about the material got a hand-off instead of an
answer, while the composer said "Chat".

Pinned workspaces (Mastery) already assert their action for this exact
reason. Do the same for unpinned ones: once the capability catalog has
loaded, reset a stored action the workspace does not offer to Chat, which
is what the composer shows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 19:11:13 +08:00

141 lines
4.7 KiB
TypeScript

"use client";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useChatStateAdapter } from "@/features/chat/ChatStateAdapter";
import { getChatCapability } from "@/features/capabilities/presentation";
import { useCapabilityCatalog } from "@/features/capabilities/useCapabilityCatalog";
import { getEnabledOptionalTools } from "@/lib/tools-settings";
/**
* Keep a workspace's action selection on the same tool policy as Home.
*
* `pinnedCapability` makes the workspace run one action and only that one:
* there is no menu, and the capability is asserted on mount so a session
* resumed with something else stored still opens on the right loop. Mastery
* uses it — its screen IS the tutor, so offering "Chat" there meant two ways
* to reach the same tutor and no way to tell which one you were in. Reading
* passes nothing and keeps the menu.
*/
export function useWorkspaceChatActions(
options: { pinnedCapability?: string } = {},
) {
const { pinnedCapability } = options;
const { state, setCapability, setTools } = useChatStateAdapter();
const { capabilities: catalogCapabilities, isLoading: catalogLoading } =
useCapabilityCatalog();
const workspaceCapabilities = useMemo(
() =>
pinnedCapability
? catalogCapabilities.filter(
(capability) => capability.value === pinnedCapability,
)
: catalogCapabilities.filter(
(capability) => capability.value !== "course_study",
),
[catalogCapabilities, pinnedCapability],
);
const [enabledOptionalTools, setEnabledOptionalTools] = useState<
string[] | null
>(null);
const activeValue = pinnedCapability
? pinnedCapability
: workspaceCapabilities.some(
(capability) => capability.value === (state.activeCapability || ""),
)
? state.activeCapability || ""
: "";
// Assert the pinned action rather than assuming it: the turn is built from
// chat state, so a session that stored another capability would otherwise
// send that one from a screen offering no way to change it.
useEffect(() => {
if (!pinnedCapability) return;
if (state.activeCapability === pinnedCapability) return;
setCapability(pinnedCapability);
}, [pinnedCapability, setCapability, state.activeCapability]);
// The same holds for an unpinned workspace: an action the menu does not
// offer (Course Study, carried in by a course hand-off) is displayed as
// Chat, so send Chat. Wait for the catalog, or a plugin action that only the
// server lists would be reset before it is known.
useEffect(() => {
if (pinnedCapability || catalogLoading) return;
if ((state.activeCapability || "") === activeValue) return;
setCapability(activeValue || null);
}, [
activeValue,
catalogLoading,
pinnedCapability,
setCapability,
state.activeCapability,
]);
const activeCapability = useMemo(
() =>
workspaceCapabilities.find(
(capability) => capability.value === activeValue,
) ?? getChatCapability(activeValue),
[activeValue, workspaceCapabilities],
);
// A pinned workspace has no menu to render.
const offeredCapabilities = pinnedCapability ? [] : workspaceCapabilities;
useEffect(() => {
let cancelled = false;
void getEnabledOptionalTools()
.then((tools) => {
if (!cancelled) setEnabledOptionalTools(tools);
})
.catch(() => {
if (!cancelled) setEnabledOptionalTools([]);
});
return () => {
cancelled = true;
};
}, []);
useEffect(() => {
if (enabledOptionalTools === null) return;
const allowed = new Set<string>(activeCapability.allowedTools);
const next = enabledOptionalTools.filter((tool) => allowed.has(tool));
const same =
next.length === state.enabledTools.length &&
next.every((tool, index) => tool === state.enabledTools[index]);
if (!same) setTools(next);
}, [
activeCapability.allowedTools,
enabledOptionalTools,
setTools,
state.enabledTools,
]);
const selectCapability = useCallback(
(value: string) => {
if (pinnedCapability) return;
const selected = workspaceCapabilities.find(
(capability) => capability.value === value,
);
const next =
selected ?? workspaceCapabilities[0] ?? getChatCapability("");
setCapability(next.value || null);
if (enabledOptionalTools !== null) {
const allowed = new Set<string>(next.allowedTools);
setTools(enabledOptionalTools.filter((tool) => allowed.has(tool)));
}
},
[
enabledOptionalTools,
pinnedCapability,
setCapability,
setTools,
workspaceCapabilities,
],
);
return {
capabilities: offeredCapabilities,
activeCapabilityValue: activeValue,
selectCapability,
};
}