mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 02:40:12 +08:00
87 lines
3.1 KiB
TypeScript
87 lines
3.1 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import {
|
|
getWorkspaceResources,
|
|
type ChatWorkspaceRegistration,
|
|
type WorkspaceResourceOption,
|
|
} from "@/lib/workspaces-api";
|
|
import { CREATIONS_SAVED_EVENT } from "@/lib/creations";
|
|
|
|
export interface ComposerResourceCatalog {
|
|
skills: WorkspaceResourceOption[];
|
|
mcp: WorkspaceResourceOption[];
|
|
}
|
|
|
|
const EMPTY: ComposerResourceCatalog = { skills: [], mcp: [] };
|
|
|
|
/**
|
|
* The skills and MCP servers this conversation may narrow itself to.
|
|
*
|
|
* The catalog endpoint answers "what can this account see", already scoped per
|
|
* workspace for skills but not for MCP, so the workspace's own allowlist is
|
|
* applied here as well. That matters beyond tidiness: the backend intersects a
|
|
* turn's selection with the same allowlist, so anything offered here that the
|
|
* workspace excludes would be a picker the user can tick to no effect.
|
|
*
|
|
* A missing allowlist (``null``) is "everything", which is what an unbound
|
|
* conversation and a workspace that never narrowed anything both have.
|
|
*/
|
|
export function useComposerResources(
|
|
workspaceId: string | null,
|
|
workspaces: ChatWorkspaceRegistration[],
|
|
): ComposerResourceCatalog {
|
|
const [catalog, setCatalog] = useState<ComposerResourceCatalog>(EMPTY);
|
|
const allowedMcp =
|
|
(workspaceId
|
|
? workspaces.find((row) => row.workspace_id === workspaceId)?.resources
|
|
?.mcp
|
|
: null) ?? null;
|
|
// Depend on the contents rather than the array identity: `workspaces` is
|
|
// refetched on every session change, and re-running on a new array of the
|
|
// same rows would refetch the catalog with it.
|
|
const allowedKey = JSON.stringify(allowedMcp);
|
|
|
|
useEffect(() => {
|
|
let alive = true;
|
|
|
|
function load(resetOnError: boolean) {
|
|
void getWorkspaceResources(workspaceId ?? "")
|
|
.then((remote) => {
|
|
if (!alive) return;
|
|
const selected = JSON.parse(allowedKey) as string[] | null;
|
|
const allowed = selected === null ? null : new Set(selected);
|
|
setCatalog({
|
|
skills: remote.skills ?? [],
|
|
mcp: (remote.mcp ?? []).filter(
|
|
(option) => allowed === null || allowed.has(option.id),
|
|
),
|
|
});
|
|
})
|
|
.catch(() => {
|
|
// The pickers simply report nothing to pick; a conversation with no
|
|
// selection inherits its workspace, so this degrades to the old
|
|
// behaviour rather than to an error the user must dismiss. A
|
|
// refetch prompted by a just-saved draft (resetOnError=false) keeps
|
|
// whatever the catalog already had instead of blanking it.
|
|
if (alive && resetOnError) setCatalog(EMPTY);
|
|
});
|
|
}
|
|
|
|
load(true);
|
|
|
|
// A skill approved from a draft card in the composer's own transcript
|
|
// would otherwise sit outside this catalog until the page reloads.
|
|
const onSaved = () => load(false);
|
|
window.addEventListener(CREATIONS_SAVED_EVENT, onSaved);
|
|
|
|
return () => {
|
|
alive = false;
|
|
window.removeEventListener(CREATIONS_SAVED_EVENT, onSaved);
|
|
};
|
|
}, [workspaceId, allowedKey]);
|
|
|
|
return catalog;
|
|
}
|