* 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>
134 lines
4.3 KiB
TypeScript
134 lines
4.3 KiB
TypeScript
/**
|
|
* Type-safe EventTarget for InspectorClient events.
|
|
* Extends the generic TypedEventTarget with InspectorClientEventMap; TypedEvent
|
|
* is provided as a type alias for use in listener signatures.
|
|
*
|
|
* v2 note: the v1.5 event map referenced SamplingCreateMessage / ElicitationCreateMessage
|
|
* classes (with embedded promise resolvers) and OAuth events from the auth subsystem.
|
|
* The auth subsystem and the runtime promise-based pending classes are not yet ported to
|
|
* v2 — see #1243 scope. Pending requests are typed with the v2 wrapper types
|
|
* (InspectorPendingSampling / InspectorPendingElicitation); OAuth events are deliberately
|
|
* omitted here and will be added when the auth subsystem is ported.
|
|
*/
|
|
|
|
import {
|
|
TypedEventTarget,
|
|
type TypedEventGeneric,
|
|
} from "./typedEventTarget.js";
|
|
import type {
|
|
ConnectionStatus,
|
|
MessageEntry,
|
|
StderrLogEntry,
|
|
FetchRequestEntry,
|
|
PromptGetInvocation,
|
|
ResourceReadInvocation,
|
|
ResourceTemplateReadInvocation,
|
|
} from "./types.js";
|
|
import type {
|
|
Tool,
|
|
ServerCapabilities,
|
|
Implementation,
|
|
Root,
|
|
Progress,
|
|
ProgressToken,
|
|
Task,
|
|
CallToolResult,
|
|
McpError,
|
|
} from "@modelcontextprotocol/sdk/types.js";
|
|
import type { InspectorPendingSampling } from "./samplingCreateMessage.js";
|
|
import type { InspectorPendingElicitation } from "./elicitationCreateMessage.js";
|
|
import type { JsonValue } from "../json/jsonUtils.js";
|
|
|
|
/** Task with createdAt optional so we can emit synthetic tasks (e.g. on result/error) that omit it. */
|
|
export type TaskWithOptionalCreatedAt = Omit<Task, "createdAt"> & {
|
|
createdAt?: string;
|
|
};
|
|
|
|
/**
|
|
* Maps event names to their detail types for CustomEvents
|
|
*/
|
|
export interface InspectorClientEventMap {
|
|
statusChange: ConnectionStatus;
|
|
toolsChange: Tool[];
|
|
capabilitiesChange: ServerCapabilities | undefined;
|
|
serverInfoChange: Implementation | undefined;
|
|
instructionsChange: string | undefined;
|
|
message: MessageEntry;
|
|
stderrLog: StderrLogEntry;
|
|
fetchRequest: FetchRequestEntry;
|
|
error: Error;
|
|
resourceUpdated: { uri: string };
|
|
progressNotification: Progress & { progressToken?: ProgressToken };
|
|
toolCallResultChange: {
|
|
toolName: string;
|
|
params: Record<string, JsonValue>;
|
|
result: CallToolResult | null;
|
|
timestamp: Date;
|
|
success: boolean;
|
|
error?: string;
|
|
metadata?: Record<string, string>;
|
|
};
|
|
resourceContentChange: {
|
|
uri: string;
|
|
content: ResourceReadInvocation;
|
|
timestamp: Date;
|
|
};
|
|
resourceTemplateContentChange: {
|
|
uriTemplate: string;
|
|
content: ResourceTemplateReadInvocation;
|
|
params: Record<string, string>;
|
|
timestamp: Date;
|
|
};
|
|
promptContentChange: {
|
|
name: string;
|
|
content: PromptGetInvocation;
|
|
timestamp: Date;
|
|
};
|
|
pendingSamplesChange: InspectorPendingSampling[];
|
|
newPendingSample: InspectorPendingSampling;
|
|
pendingElicitationsChange: InspectorPendingElicitation[];
|
|
newPendingElicitation: InspectorPendingElicitation;
|
|
rootsChange: Root[];
|
|
resourceSubscriptionsChange: string[];
|
|
// Task events
|
|
/** Fired only from server notification notifications/tasks/status. */
|
|
taskStatusChange: { taskId: string; task: Task };
|
|
/** Fired from callToolStream for each task update. */
|
|
toolCallTaskUpdated: {
|
|
taskId: string;
|
|
task: TaskWithOptionalCreatedAt;
|
|
result?: CallToolResult;
|
|
error?: McpError;
|
|
};
|
|
/** Fired from getRequestorTask() and callToolStream (client-origin task updates). */
|
|
requestorTaskUpdated: {
|
|
taskId: string;
|
|
task: TaskWithOptionalCreatedAt;
|
|
result?: CallToolResult;
|
|
error?: McpError;
|
|
};
|
|
taskCancelled: { taskId: string };
|
|
tasksChange: Task[];
|
|
// Signal events (no payload)
|
|
connect: void;
|
|
disconnect: void;
|
|
// List changed notification events
|
|
toolsListChanged: void;
|
|
resourcesListChanged: void;
|
|
resourceTemplatesListChanged: void;
|
|
promptsListChanged: void;
|
|
tasksListChanged: void;
|
|
// Session persistence (dispatched by client; FetchRequestLogState listens and saves)
|
|
saveSession: { sessionId: string };
|
|
}
|
|
|
|
/**
|
|
* Type alias for InspectorClient typed events (for listener signatures).
|
|
*/
|
|
export type TypedEvent<K extends keyof InspectorClientEventMap> =
|
|
TypedEventGeneric<InspectorClientEventMap, K>;
|
|
|
|
/**
|
|
* Type-safe EventTarget for InspectorClient events.
|
|
*/
|
|
export class InspectorClientEventTarget extends TypedEventTarget<InspectorClientEventMap> {}
|