* 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
156
core/mcp/state/fetchRequestLogState.ts
Normal file
156
core/mcp/state/fetchRequestLogState.ts
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
/**
|
||||
* FetchRequestLogState: holds the fetch request log, subscribes to the protocol
|
||||
* "fetchRequest" event. Protocol emits per-entry; this manager owns the list and
|
||||
* emits both `fetchRequest` (single entry) and `fetchRequestsChange` (full list)
|
||||
* on append, and `fetchRequestsChange` on clear. Does not clear on connect or
|
||||
* disconnect.
|
||||
*
|
||||
* When `sessionStorage` and `sessionId` are provided, restores fetch requests
|
||||
* from storage on creation and listens for the client's `saveSession` event to
|
||||
* persist before OAuth redirect.
|
||||
*
|
||||
* 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 { FetchRequestEntry } from "../types.js";
|
||||
import type {
|
||||
InspectorClientStorage,
|
||||
InspectorClientSessionState,
|
||||
} from "../sessionStorage.js";
|
||||
import type { InspectorClientEventMap } from "../inspectorClientEventTarget.js";
|
||||
import {
|
||||
TypedEventTarget,
|
||||
type TypedEventGeneric,
|
||||
} from "../typedEventTarget.js";
|
||||
|
||||
export interface FetchRequestLogStateEventMap {
|
||||
fetchRequest: FetchRequestEntry;
|
||||
fetchRequestsChange: FetchRequestEntry[];
|
||||
}
|
||||
|
||||
export interface FetchRequestLogStateOptions {
|
||||
/**
|
||||
* Maximum number of fetch requests to store (0 = unlimited, not recommended).
|
||||
* When exceeded, oldest entries are dropped. Default 1000.
|
||||
*/
|
||||
maxFetchRequests?: number;
|
||||
/**
|
||||
* When provided with sessionId, fetch requests are restored from storage on
|
||||
* creation and saved when the client dispatches `saveSession` (e.g. before
|
||||
* OAuth redirect).
|
||||
*/
|
||||
sessionStorage?: InspectorClientStorage;
|
||||
/** Session ID for load/save; required for sessionStorage to have effect. */
|
||||
sessionId?: string;
|
||||
}
|
||||
|
||||
export class FetchRequestLogState extends TypedEventTarget<FetchRequestLogStateEventMap> {
|
||||
private fetchRequests: FetchRequestEntry[] = [];
|
||||
private client: InspectorClientProtocol | null = null;
|
||||
private unsubscribe: (() => void) | null = null;
|
||||
private readonly maxFetchRequests: number;
|
||||
|
||||
constructor(
|
||||
client: InspectorClientProtocol,
|
||||
options: FetchRequestLogStateOptions = {},
|
||||
) {
|
||||
super();
|
||||
this.maxFetchRequests = options.maxFetchRequests ?? 1000;
|
||||
this.client = client;
|
||||
|
||||
const onFetchRequest = (
|
||||
event: TypedEventGeneric<InspectorClientEventMap, "fetchRequest">,
|
||||
): void => {
|
||||
const entry = event.detail;
|
||||
if (
|
||||
this.maxFetchRequests > 0 &&
|
||||
this.fetchRequests.length >= this.maxFetchRequests
|
||||
) {
|
||||
this.fetchRequests.shift();
|
||||
}
|
||||
this.fetchRequests.push(entry);
|
||||
this.dispatchTypedEvent("fetchRequest", entry);
|
||||
this.dispatchTypedEvent("fetchRequestsChange", this.getFetchRequests());
|
||||
};
|
||||
this.client.addEventListener("fetchRequest", onFetchRequest);
|
||||
|
||||
const sessionStorage = options.sessionStorage;
|
||||
const sessionId = options.sessionId;
|
||||
|
||||
if (sessionStorage) {
|
||||
const onSaveSession = (
|
||||
event: TypedEventGeneric<InspectorClientEventMap, "saveSession">,
|
||||
): void => {
|
||||
const { sessionId: id } = event.detail;
|
||||
const state: InspectorClientSessionState = {
|
||||
fetchRequests: this.getFetchRequests(),
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
};
|
||||
sessionStorage.saveSession(id, state).catch(() => {
|
||||
// fire-and-forget; storage may log internally
|
||||
});
|
||||
};
|
||||
this.client.addEventListener("saveSession", onSaveSession);
|
||||
this.unsubscribe = () => {
|
||||
if (this.client) {
|
||||
this.client.removeEventListener("fetchRequest", onFetchRequest);
|
||||
this.client.removeEventListener("saveSession", onSaveSession);
|
||||
}
|
||||
this.client = null;
|
||||
};
|
||||
if (sessionId) {
|
||||
sessionStorage
|
||||
.loadSession(sessionId)
|
||||
.then((state) => {
|
||||
if (this.client && state?.fetchRequests?.length) {
|
||||
this.hydrateFetchRequests(state.fetchRequests);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
// fire-and-forget; storage may log internally. Matches the
|
||||
// saveSession swallow above so a corrupt or unreadable session
|
||||
// doesn't surface as an unhandled rejection.
|
||||
});
|
||||
}
|
||||
} else {
|
||||
this.unsubscribe = () => {
|
||||
if (this.client) {
|
||||
this.client.removeEventListener("fetchRequest", onFetchRequest);
|
||||
}
|
||||
this.client = null;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private hydrateFetchRequests(entries: FetchRequestEntry[]): void {
|
||||
const trimmed =
|
||||
this.maxFetchRequests > 0
|
||||
? entries.slice(-this.maxFetchRequests)
|
||||
: entries;
|
||||
this.fetchRequests = trimmed;
|
||||
this.dispatchTypedEvent("fetchRequestsChange", this.getFetchRequests());
|
||||
}
|
||||
|
||||
getFetchRequests(): FetchRequestEntry[] {
|
||||
return [...this.fetchRequests];
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all fetch requests. Dispatches fetchRequestsChange only if the list
|
||||
* was non-empty.
|
||||
*/
|
||||
clearFetchRequests(): void {
|
||||
if (this.fetchRequests.length === 0) return;
|
||||
this.fetchRequests = [];
|
||||
this.dispatchTypedEvent("fetchRequestsChange", []);
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.unsubscribe?.();
|
||||
this.unsubscribe = null;
|
||||
this.fetchRequests = [];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue