mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* feat(lens): add trace_agents rollup query * feat(lens): register the trace_agents read query * feat(lens): add trace_agents params and row types * feat(lens): dispatch the trace_agents query * feat(lens): export trace_agents wire schemas * test(lens): pin the trace_agents query name * test(lens): cover trace_agents scope, window and failure counts * feat(lens): cap the agent list size * feat(lens): declare the trace_agents bridge query * feat(lens): read trace agents from clickhouse storage * feat(lens): add trace agent response models * feat(lens): list agents within the reader's trace scope * feat(lens): add GET /v1/traces/agents * chore(lens): regenerate trace models with trace_agents * chore(lens): regenerate trace types with trace_agents * chore(lens): regenerate read query name schema * chore(lens): add trace agent row schema * chore(lens): add trace agents params schema * test(lens): cover the trace agents route * test(lens): cover agent listing scope and timestamps * chore(ui): regenerate api types with trace agents route * feat(lens): derive trace agent types from the schema * feat(lens): fetch the agent list from the traces api * feat(lens): roll up demo runs into agents * feat(lens): serve the agent list in demo data * feat(lens): load agents seen in the last two weeks * feat(lens): remember the selected agent per browser * feat(lens): add the agent picker * feat(lens): wire agent selection into the lens header * feat(lens): show the agent picker next to the lens title * refactor(lens): drop the toolbar agent filter in favor of the header picker * test(lens): remove tests for the toolbar agent filter * test(lens): cover agent resolution and rollup * test(lens): cover scoping, switching and remembering the agent * test(lens): read only the create request in guided setup * test(lens): assert the toolbar agent filter is gone * test(lens): read stubbed requests without their abort signal
97 lines
3.6 KiB
TypeScript
97 lines
3.6 KiB
TypeScript
import type { ReactElement } from "react";
|
|
import { vi } from "vitest";
|
|
import { LensServicesProvider, liveLensServices } from "@/components/lens/data/LensServices";
|
|
import { OnboardingProvider, type Onboarding } from "@/components/lens/onboarding/OnboardingContext";
|
|
import { renderWithProviders } from "./test-utils";
|
|
|
|
export interface StubbedRequest {
|
|
readonly path: string;
|
|
readonly method: string;
|
|
readonly query: URLSearchParams;
|
|
readonly body: unknown;
|
|
readonly authorization: string | null;
|
|
}
|
|
|
|
/** The path of a stubbed fetch call, whether a client passed a URL or a Request. */
|
|
export const requestPath = (input: RequestInfo | URL): string =>
|
|
new URL(input instanceof Request ? input.url : String(input), "http://localhost").pathname;
|
|
|
|
/** Reads a stubbed fetch call the same way whether a client passed a URL and init or a Request. */
|
|
export async function readRequest(input: RequestInfo | URL, init?: RequestInit): Promise<StubbedRequest> {
|
|
const request =
|
|
input instanceof Request
|
|
? input
|
|
: new Request(new URL(String(input), "http://localhost"), { ...init, signal: undefined });
|
|
const url = new URL(request.url);
|
|
const text = await request.clone().text();
|
|
return {
|
|
path: url.pathname,
|
|
method: request.method,
|
|
query: url.searchParams,
|
|
body: text ? JSON.parse(text) : undefined,
|
|
authorization: request.headers.get("Authorization"),
|
|
};
|
|
}
|
|
|
|
export interface GatewayRequest {
|
|
readonly query: Readonly<Record<string, string>>;
|
|
readonly body: unknown;
|
|
readonly authorization: string | null;
|
|
}
|
|
|
|
type GatewayHandler = (path: string, request: GatewayRequest) => unknown;
|
|
|
|
/**
|
|
* Fakes the proxy at fetch, so Lens code runs its real HTTP clients. Each method's handler gets the path and the
|
|
* parsed query and body; a rejection becomes a 500 carrying its message, and a pending promise stays pending.
|
|
*/
|
|
export function stubGateway() {
|
|
const gateway = {
|
|
get: vi.fn<GatewayHandler>(),
|
|
post: vi.fn<GatewayHandler>(),
|
|
put: vi.fn<GatewayHandler>(),
|
|
patch: vi.fn<GatewayHandler>(),
|
|
delete: vi.fn<GatewayHandler>(),
|
|
};
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn<typeof fetch>(async (input, init) => {
|
|
const { path, method, query, body, authorization } = await readRequest(input, init);
|
|
const handler = gateway[method.toLowerCase() as keyof typeof gateway];
|
|
try {
|
|
return Response.json((await handler(path, { query: Object.fromEntries(query), body, authorization })) ?? null);
|
|
} catch (error) {
|
|
return Response.json({ detail: error instanceof Error ? error.message : String(error) }, { status: 500 });
|
|
}
|
|
}),
|
|
);
|
|
return gateway;
|
|
}
|
|
|
|
type LensRenderOptions = Parameters<typeof renderWithProviders>[1] & {
|
|
accessToken?: string;
|
|
onboarding?: Partial<Onboarding>;
|
|
};
|
|
|
|
/** Lens components read their API from context, so standalone renders need the live services for a token. */
|
|
export function renderWithLens(ui: ReactElement, options?: LensRenderOptions) {
|
|
const { accessToken = "test", onboarding: overrides, ...renderOptions } = options ?? {};
|
|
const services = liveLensServices(accessToken);
|
|
const onboarding: Onboarding = {
|
|
readOnly: false,
|
|
canViewInvestigations: true,
|
|
canInvestigate: true,
|
|
canMintTracingKey: true,
|
|
connect: () => {},
|
|
create: () => {},
|
|
openTrace: () => {},
|
|
...overrides,
|
|
};
|
|
const wrap = (element: ReactElement) => (
|
|
<LensServicesProvider services={services}>
|
|
<OnboardingProvider value={onboarding}>{element}</OnboardingProvider>
|
|
</LensServicesProvider>
|
|
);
|
|
const view = renderWithProviders(wrap(ui), renderOptions);
|
|
return { ...view, rerender: (next: ReactElement) => view.rerender(wrap(next)) };
|
|
}
|