* feat(core): port v1.5 hook layer (useInspectorClient + useManaged*/usePaged* + log hooks) Wholesale port of v1.5's React hook layer and the EventTarget-based state machinery the hooks subscribe to (#1243). Hooks decouple from the concrete InspectorClient via a new `InspectorClientProtocol` interface so this layer can land before the runtime class is ported. Added under `core/`: - `core/react/` — useInspectorClient + 5 useManaged* + 5 usePaged* + 3 log hooks - `core/mcp/state/` — 5 managed*State + 5 paged*State + messageLogState + stderrLogState + fetchRequestLogState - `core/mcp/inspectorClientProtocol.ts`, `inspectorClientEventTarget.ts`, `typedEventTarget.ts`, `samplingCreateMessage.ts`, `sessionStorage.ts` - v2-only wrapper types in `core/mcp/types.ts`: InspectorUrlElicitRequest, InspectorPendingRequest, InspectorRequestHistoryItem Tests live under `clients/web/src/test/core/` (28 new test files, 264 new tests). Test fake: `core/mcp/__tests__/fakeInspectorClient.ts`. Build wiring: - `vite.config.ts`: dedupe react/react-dom so core/ files resolve them via clients/web/node_modules. - `tsconfig.app.json` / `tsconfig.test.json`: paths map react/react-dom/vitest to clients/web/node_modules so `tsc -b` resolves bare imports from core/ source files (which sit above the package's node_modules). Exclude core/__tests__ from app config; include in test config. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * review: address PR #1300 review findings Bugs: - fetchRequestLogState: add .catch() on sessionStorage.loadSession so a corrupt or unreadable session doesn't surface as an unhandled rejection (matches the saveSession swallow pattern). - messageLogState: sync pendingRequestEntries when clearMessages() evicts a pending request entry, so a later response with the same id falls through to the append branch instead of mutating an unreachable map entry. Add tests for both predicate and no-predicate paths. Cleanups: - Extract mergeTaskIntoList helper to core/mcp/state/mergeTaskIntoList.ts so managed/paged RequestorTasks state classes share one definition. - Add a one-line JSDoc note on each PagedTools/Prompts/Resources/Templates state explaining why they deliberately do not subscribe to *ListChanged (cursor invalidation mid-pagination). - Document on useInspectorClient that appRendererClient is read lazily on every render and is not subscribed (changes once at connect time). Carry-overs from v1.5 deliberately left for the follow-up InspectorClient runtime port: - Refresh/status-change race in Managed*State.refresh() (status check is only at entry; a disconnect during await can re-clear before refresh resumes). - nextCursor exposure asymmetry across usePaged* (only usePagedRequestorTasks surfaces it via state field; others return it from loadPage() only). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a4215e57b9
commit
e3e70470b5
1 changed files with 6404 additions and 5 deletions
136
core/mcp/state/managedRequestorTasksState.ts
Normal file
136
core/mcp/state/managedRequestorTasksState.ts
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
/**
|
||||
* ManagedRequestorTasksState: holds full requestor task list, syncs on
|
||||
* tasksListChanged. Subscribes to taskStatusChange (server) and
|
||||
* requestorTaskUpdated (client) to merge per-task updates.
|
||||
*
|
||||
* Ported from v1.5/main. v2 substitutes `InspectorClientProtocol` for the
|
||||
* concrete `InspectorClient` since the runtime class is not yet ported.
|
||||
*/
|
||||
|
||||
import type { InspectorClientProtocol } from "../inspectorClientProtocol.js";
|
||||
import type { Task } from "@modelcontextprotocol/sdk/types.js";
|
||||
import type { InspectorClientEventMap } from "../inspectorClientEventTarget.js";
|
||||
import {
|
||||
TypedEventTarget,
|
||||
type TypedEventGeneric,
|
||||
} from "../typedEventTarget.js";
|
||||
import { mergeTaskIntoList } from "./mergeTaskIntoList.js";
|
||||
|
||||
const MAX_PAGES = 100;
|
||||
|
||||
export interface ManagedRequestorTasksStateEventMap {
|
||||
tasksChange: Task[];
|
||||
}
|
||||
|
||||
/**
|
||||
* State manager that keeps the full requestor task list in sync with the
|
||||
* server. Subscribes to connect, tasksListChanged, statusChange,
|
||||
* taskStatusChange (server), requestorTaskUpdated (client), and taskCancelled.
|
||||
*/
|
||||
export class ManagedRequestorTasksState extends TypedEventTarget<ManagedRequestorTasksStateEventMap> {
|
||||
private tasks: Task[] = [];
|
||||
private client: InspectorClientProtocol | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
|
||||
constructor(client: InspectorClientProtocol) {
|
||||
super();
|
||||
this.client = client;
|
||||
const onConnect = (): void => {
|
||||
void this.refresh();
|
||||
};
|
||||
const onTasksListChanged = (): void => {
|
||||
void this.refresh();
|
||||
};
|
||||
const onStatusChange = (): void => {
|
||||
if (this.client?.getStatus() === "disconnected") {
|
||||
this.tasks = [];
|
||||
this.dispatchTypedEvent("tasksChange", []);
|
||||
}
|
||||
};
|
||||
const onTaskStatusChange = (
|
||||
e: TypedEventGeneric<InspectorClientEventMap, "taskStatusChange">,
|
||||
): void => {
|
||||
const { taskId, task } = e.detail;
|
||||
this.tasks = mergeTaskIntoList(this.tasks, taskId, task);
|
||||
this.dispatchTypedEvent("tasksChange", this.tasks);
|
||||
};
|
||||
const onRequestorTaskUpdated = (
|
||||
e: TypedEventGeneric<InspectorClientEventMap, "requestorTaskUpdated">,
|
||||
): void => {
|
||||
const { taskId, task } = e.detail;
|
||||
this.tasks = mergeTaskIntoList(this.tasks, taskId, task);
|
||||
this.dispatchTypedEvent("tasksChange", this.tasks);
|
||||
};
|
||||
const onTaskCancelled = (
|
||||
e: TypedEventGeneric<InspectorClientEventMap, "taskCancelled">,
|
||||
): void => {
|
||||
const { taskId } = e.detail;
|
||||
const idx = this.tasks.findIndex((t) => t.taskId === taskId);
|
||||
if (idx >= 0) {
|
||||
const next = [...this.tasks];
|
||||
const prev = next[idx]!;
|
||||
next[idx] = { ...prev, status: "cancelled" as const };
|
||||
this.tasks = next;
|
||||
this.dispatchTypedEvent("tasksChange", this.tasks);
|
||||
}
|
||||
};
|
||||
this.client.addEventListener("connect", onConnect);
|
||||
this.client.addEventListener("tasksListChanged", onTasksListChanged);
|
||||
this.client.addEventListener("statusChange", onStatusChange);
|
||||
this.client.addEventListener("taskStatusChange", onTaskStatusChange);
|
||||
this.client.addEventListener(
|
||||
"requestorTaskUpdated",
|
||||
onRequestorTaskUpdated,
|
||||
);
|
||||
this.client.addEventListener("taskCancelled", onTaskCancelled);
|
||||
this.unsubscribe = () => {
|
||||
if (this.client) {
|
||||
this.client.removeEventListener("connect", onConnect);
|
||||
this.client.removeEventListener("tasksListChanged", onTasksListChanged);
|
||||
this.client.removeEventListener("statusChange", onStatusChange);
|
||||
this.client.removeEventListener("taskStatusChange", onTaskStatusChange);
|
||||
this.client.removeEventListener(
|
||||
"requestorTaskUpdated",
|
||||
onRequestorTaskUpdated,
|
||||
);
|
||||
this.client.removeEventListener("taskCancelled", onTaskCancelled);
|
||||
}
|
||||
this.client = null;
|
||||
};
|
||||
}
|
||||
|
||||
getTasks(): Task[] {
|
||||
return [...this.tasks];
|
||||
}
|
||||
|
||||
async refresh(): Promise<Task[]> {
|
||||
const client = this.client;
|
||||
if (!client || client.getStatus() !== "connected") {
|
||||
return this.getTasks();
|
||||
}
|
||||
this.tasks = [];
|
||||
let cursor: string | undefined;
|
||||
let pageCount = 0;
|
||||
do {
|
||||
const result = await client.listRequestorTasks(cursor);
|
||||
this.tasks = cursor
|
||||
? [...this.tasks, ...result.tasks]
|
||||
: result.tasks;
|
||||
cursor = result.nextCursor;
|
||||
pageCount++;
|
||||
if (pageCount >= MAX_PAGES) {
|
||||
throw new Error(
|
||||
`Maximum pagination limit (${MAX_PAGES} pages) reached while listing requestor tasks`,
|
||||
);
|
||||
}
|
||||
} while (cursor);
|
||||
this.dispatchTypedEvent("tasksChange", this.tasks);
|
||||
return this.getTasks();
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.tasks = [];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue