@mastra/core
Version:
277 lines • 13.7 kB
TypeScript
/**
* Storage contract for querying individual completed spans across traces.
*
* Initial visibility: completed records only, consistent with insert-only ClickHouse.
* Current-record selection precedes mutable predicates and pagination. A store must
* define its version rule; endedAt alone is not a general-purpose revision number.
*/
import { z } from 'zod/v4';
import type { TraceQueryPlanOptions, TrustedTraceQueryScalarPredicate } from './trace-query.js';
/**
* @deprecated Span query previews use the shared preview format (`buildPreview`), at most
* `PREVIEW_MAX_LENGTH` characters plus `…`.
*/
export declare const SPAN_QUERY_MAX_PREVIEW_CHARACTERS = 256;
export declare const SPAN_QUERY_MAX_LIMIT = 1000;
export declare const SPAN_QUERY_MAX_CURSOR_BYTES = 8192;
/** Span-only fields do not extend the trace-query ordering contract. */
export declare const SPAN_QUERY_ORDER_FIELDS: readonly ["startedAt", "endedAt", "timeToFirstChunkMs", "timeToFirstTokenMs", "durationMs", "model", "provider"];
export interface SpanQueryPlanOptions extends TraceQueryPlanOptions {
/** Opt in only when the host checks span-query-duration-sort support before execution. */
allowSpanDurationOrdering?: boolean;
/** Opt in only when the host checks span-query-model-provider-sort support before execution. */
allowSpanModelProviderOrdering?: boolean;
}
declare const requestSchema: z.ZodObject<{
timeRange: z.ZodObject<{
from: z.ZodString;
to: z.ZodString;
}, z.core.$strict>;
where: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>;
orderBy: z.ZodDefault<z.ZodArray<z.ZodObject<{
field: z.ZodEnum<{
durationMs: "durationMs";
endedAt: "endedAt";
model: "model";
provider: "provider";
startedAt: "startedAt";
timeToFirstChunkMs: "timeToFirstChunkMs";
timeToFirstTokenMs: "timeToFirstTokenMs";
}>;
direction: z.ZodEnum<{
asc: "asc";
desc: "desc";
}>;
}, z.core.$strict>>>;
page: z.ZodDefault<z.ZodObject<{
limit: z.ZodDefault<z.ZodNumber>;
after: z.ZodOptional<z.ZodString>;
}, z.core.$strict>>;
}, z.core.$strict>;
export declare const spanQueryRequestSchema: z.ZodPreprocess<z.ZodObject<{
timeRange: z.ZodObject<{
from: z.ZodString;
to: z.ZodString;
}, z.core.$strict>;
where: z.ZodOptional<z.ZodType<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate, z.core.$ZodTypeInternals<import("./trace-query").TraceQueryScalarPredicate, import("./trace-query").TraceQueryScalarPredicate>>>;
orderBy: z.ZodDefault<z.ZodArray<z.ZodObject<{
field: z.ZodEnum<{
durationMs: "durationMs";
endedAt: "endedAt";
model: "model";
provider: "provider";
startedAt: "startedAt";
timeToFirstChunkMs: "timeToFirstChunkMs";
timeToFirstTokenMs: "timeToFirstTokenMs";
}>;
direction: z.ZodEnum<{
asc: "asc";
desc: "desc";
}>;
}, z.core.$strict>>>;
page: z.ZodDefault<z.ZodObject<{
limit: z.ZodDefault<z.ZodNumber>;
after: z.ZodOptional<z.ZodString>;
}, z.core.$strict>>;
}, z.core.$strict>, unknown>;
export declare const spanQueryIdentitySchema: z.ZodObject<{
organizationId: z.ZodNullable<z.ZodString>;
resourceId: z.ZodNullable<z.ZodString>;
traceId: z.ZodString;
spanId: z.ZodString;
}, z.core.$strict>;
export declare const spanQueryCostSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
state: z.ZodLiteral<"available">;
amount: z.ZodNumber;
currency: z.ZodString;
}, z.core.$strict>, z.ZodObject<{
state: z.ZodLiteral<"missing">;
}, z.core.$strict>, z.ZodObject<{
state: z.ZodLiteral<"unavailable">;
}, z.core.$strict>], "state">;
export declare const spanQueryRowSchema: z.ZodObject<{
organizationId: z.ZodNullable<z.ZodString>;
resourceId: z.ZodNullable<z.ZodString>;
traceId: z.ZodString;
spanId: z.ZodString;
parentSpanId: z.ZodNullable<z.ZodString>;
name: z.ZodString;
spanType: z.ZodString;
status: z.ZodEnum<{
error: "error";
success: "success";
}>;
startedAt: z.ZodString;
endedAt: z.ZodString;
durationMs: z.ZodNumber;
entityType: z.ZodNullable<z.ZodString>;
entityId: z.ZodNullable<z.ZodString>;
entityName: z.ZodNullable<z.ZodString>;
entityVersionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityType: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityName: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityVersionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityType: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityName: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityVersionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
threadId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
runId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
sessionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
userId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
requestId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
serviceName: z.ZodDefault<z.ZodNullable<z.ZodString>>;
experimentId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
environment: z.ZodDefault<z.ZodNullable<z.ZodString>>;
executionSource: z.ZodDefault<z.ZodNullable<z.ZodString>>;
model: z.ZodNullable<z.ZodString>;
provider: z.ZodNullable<z.ZodString>;
inputPreview: z.ZodNullable<z.ZodString>;
outputPreview: z.ZodNullable<z.ZodString>;
errorPreview: z.ZodNullable<z.ZodString>;
timeToFirstChunkMs: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
timeToFirstTokenMs: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
outcome: z.ZodOptional<z.ZodNullable<z.ZodString>>;
stopReason: z.ZodOptional<z.ZodNullable<z.ZodString>>;
stopReasonContext: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
}, z.core.$strict>;
export declare const spanQueryResponseSchema: z.ZodObject<{
spans: z.ZodArray<z.ZodObject<{
organizationId: z.ZodNullable<z.ZodString>;
resourceId: z.ZodNullable<z.ZodString>;
traceId: z.ZodString;
spanId: z.ZodString;
parentSpanId: z.ZodNullable<z.ZodString>;
name: z.ZodString;
spanType: z.ZodString;
status: z.ZodEnum<{
error: "error";
success: "success";
}>;
startedAt: z.ZodString;
endedAt: z.ZodString;
durationMs: z.ZodNumber;
entityType: z.ZodNullable<z.ZodString>;
entityId: z.ZodNullable<z.ZodString>;
entityName: z.ZodNullable<z.ZodString>;
entityVersionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityType: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityName: z.ZodDefault<z.ZodNullable<z.ZodString>>;
parentEntityVersionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityType: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityName: z.ZodDefault<z.ZodNullable<z.ZodString>>;
rootEntityVersionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
threadId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
runId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
sessionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
userId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
requestId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
serviceName: z.ZodDefault<z.ZodNullable<z.ZodString>>;
experimentId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
environment: z.ZodDefault<z.ZodNullable<z.ZodString>>;
executionSource: z.ZodDefault<z.ZodNullable<z.ZodString>>;
model: z.ZodNullable<z.ZodString>;
provider: z.ZodNullable<z.ZodString>;
inputPreview: z.ZodNullable<z.ZodString>;
outputPreview: z.ZodNullable<z.ZodString>;
errorPreview: z.ZodNullable<z.ZodString>;
timeToFirstChunkMs: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
timeToFirstTokenMs: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
outcome: z.ZodOptional<z.ZodNullable<z.ZodString>>;
stopReason: z.ZodOptional<z.ZodNullable<z.ZodString>>;
stopReasonContext: z.ZodOptional<z.ZodNullable<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
}, z.core.$strict>>;
page: z.ZodObject<{
next: z.ZodNullable<z.ZodString>;
}, z.core.$strict>;
}, z.core.$strict>;
export type SpanQueryRequest = z.input<typeof requestSchema>;
export type NormalizedSpanQueryRequest = z.output<typeof requestSchema>;
export type SpanQueryIdentity = z.infer<typeof spanQueryIdentitySchema>;
export type SpanQueryRow = z.infer<typeof spanQueryRowSchema>;
export type SpanQueryResponse = z.infer<typeof spanQueryResponseSchema>;
declare const cursorValuesSchema: z.ZodObject<{
organizationId: z.ZodNullable<z.ZodString>;
resourceId: z.ZodNullable<z.ZodString>;
traceId: z.ZodString;
spanId: z.ZodString;
sortValue: z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodNull]>;
}, z.core.$strict>;
/**
* Ordering semantics every store compiler must implement:
* - Sort by `orderBy.field` in the requested direction, then break ties by
* organizationId, resourceId, traceId, and spanId, in that order, all ascending.
* Missing sort values sort last in both directions and tie with each other.
* Compare model/provider strings lexicographically by UTF-8 bytes, case sensitively.
* Empty strings are values, not missing values.
* - Compare non-null IDs lexicographically by UTF-8 bytes, without locale collation.
* Null organizationId/resourceId values sort last and compare equal to other nulls.
* - Resume strictly after the cursor using that same complete ordering. Apply the
* requested direction only to the sort value; identity tie-breaks remain ascending.
* Use null-safe equality/comparison for tenant fields, not a raw nullable SQL tuple.
* - Select and page unique current span identities before hydrating previews or cost.
*/
export interface TrustedSpanQueryPlan {
result: 'spans';
timeRange: {
from: string;
to: string;
};
where?: TrustedTraceQueryScalarPredicate;
scope?: TraceQueryPlanOptions['scope'];
orderBy: NormalizedSpanQueryRequest['orderBy'][number];
limit: number;
binding: string;
cursor?: z.infer<typeof cursorValuesSchema>;
}
export declare function parseSpanQueryRequest(input: unknown): NormalizedSpanQueryRequest;
/** Host authorization is separate from the query document; a binding is not an authorization grant. */
export declare function planSpanQuery(input: unknown, options?: SpanQueryPlanOptions): TrustedSpanQueryPlan;
export declare function encodeSpanQueryCursor(plan: TrustedSpanQueryPlan, values: z.infer<typeof cursorValuesSchema>): string;
/**
* Span query preview from a stored preview (see `buildPreview`): `''` (nothing to preview) and
* `null` become `null`, and `truncated` says whether the preview was cut.
*/
export declare function createSpanQueryPreview(preview: string | null): {
value: string | null;
truncated: boolean;
};
/** @internal Identity shared by page hydration and metric attribution. */
export declare function spanQueryIdentityKey(identity: SpanQueryIdentity): string;
/** @internal Bounded, deduplicated token metrics used to resolve per-span model cost. */
export interface SpanQueryCostMetric extends SpanQueryIdentity {
name: string;
estimatedCost: number | null;
costUnit: string | null;
allocation: string | null;
costError: boolean;
}
/** @internal Fail rather than return a partial, misleading cost for an oversized page. */
export declare const SPAN_QUERY_MAX_COST_METRICS = 50000;
/**
* @internal Total token metrics are authoritative; detail metrics overlap them.
* A provider-supplied query_total carrier replaces both directional totals.
* Missing means no token metrics have arrived; partial/unpriced/ambiguous cost is unavailable.
*/
export declare function resolveSpanQueryCost(metrics: SpanQueryCostMetric[]): z.infer<typeof spanQueryCostSchema>;
/** @internal Selected page metadata; adapters normalize dates to ISO strings. */
export type SpanQuerySelectedRow = Omit<SpanQueryRow, 'inputPreview' | 'outputPreview' | 'errorPreview'> & {
/** Keep the selected column's boundary when hydration adds a legacy display fallback. */
cursorSortValue?: string | number | null;
};
/**
* @internal Previews built by `buildPreview`: the stored preview columns, or for rows written
* before a store kept them, a preview built from the payload column.
*/
export interface SpanQueryPayload extends SpanQueryIdentity {
inputPreview: string | null;
outputPreview: string | null;
errorPreview: string | null;
}
/** @internal Hydration never changes the selected page's order or cursor boundary. */
export declare function buildSpanQueryResponse(plan: TrustedSpanQueryPlan, selected: SpanQuerySelectedRow[], payloads: SpanQueryPayload[], _metrics?: SpanQueryCostMetric[]): SpanQueryResponse;
export {};
//# sourceMappingURL=span-query.d.ts.map