Files
webstudio/packages/content-engine/src/query-error.ts
T
Oleg Isonen 2ec78c183d feat: Add single-result modes to Assets queries (#6040)
## Summary

- Add `many`, `one`, `first`, and `last` result modes to Assets queries
while keeping `many` as the backward-compatible default.
- Return direct items for single-result modes, validate deterministic
ordering, and report duplicate `one` matches as a structured
`MULTIPLE_RESULTS` error.
- Update Builder controls, preview/published runtime handling, SDK
resource encoding, MCP/CLI guidance, generated contracts, and fixtures.

## Implementation TODOs

- [x] Add result-mode schemas and core execution behavior.
- [x] Select single results before content hydration and preserve
deterministic ID tie-breaking.
- [x] Add structured duplicate-match transport errors.
- [x] Update Builder controls and hide/reset pagination for
single-result modes.
- [x] Keep existing settings first, then Output and Result, without a
duplicate Return label.
- [x] Preserve legacy saved resources with an implicit `many` default.
- [x] Update preview, published runtime, SDK, protocol, MCP, and CLI
behavior.
- [x] Regenerate runtime contracts, server metadata, CLI docs, and
fixtures.
- [x] Review documentation impact and update the authoritative MCP/CLI
guidance.
- [x] Add regression coverage and verify the new tests fail without the
implementation.
- [ ] Run the agent evaluation suite (not run; requires explicit user
approval).

## Compatibility and behavior

- Existing queries without `result` continue to behave as `many` and
retain the ID-keyed collection shape.
- `one` returns an item or `null`, but fails when more than one asset
matches.
- `first` and `last` require explicit sorting; the existing asset-ID
tie-breaker makes equal sort values deterministic.
- Single-result modes ignore and do not persist pagination controls.
Materialized collection queries remain collection-only; single-result
queries execute dynamically.

## Verification

- Content engine typecheck and full test suite (291 tests).
- Query builder, query-builder React, SDK, protocol, asset uploader,
project build, Builder, and CLI targeted typechecks/tests.
- Root lint.
- MCP documentation tests, typecheck, sync check, and generated-doc
checks.
- Generated API contract checks.
- Fixture generation pipeline.
- Final focused content-engine regression run (53 tests).
- Final DRY review: affected typechecks, focused tests, lint, package
boundaries, generated checks, and fixtures.

Closes #6039
2026-08-07 19:13:19 +02:00

98 lines
2.7 KiB
TypeScript

import type { AssetResourceQueryFailure } from "./schema";
import { AssetResourceHydrationError } from "./hydration";
import {
AssetIndexRevisionError,
AssetQueryExecutionError,
AssetQueryMultipleResultsError,
} from "./structured-query";
import { DocumentResolutionLimitError } from "./document-graph/document-resolution";
import { CachedDocumentLoaderError } from "./document-graph/cached-document-loader";
export type AssetResourceQueryError = Omit<
AssetResourceQueryFailure["error"],
"details"
> & {
details?: Record<string, string | number>;
status: 400 | 409;
};
/** Classifies content-query failures independently of an HTTP or RPC transport. */
export const getAssetResourceQueryError = (
error: unknown
): AssetResourceQueryError | undefined => {
if (error instanceof AssetIndexRevisionError) {
return {
code: "STALE_INDEX",
message: error.message,
retryable: false,
status: 409,
};
}
if (error instanceof AssetQueryMultipleResultsError) {
return {
code: error.code,
message: error.message,
retryable: false,
details: { matchedCount: error.matchedCount },
status: 409,
};
}
if (error instanceof AssetQueryExecutionError) {
return {
code: "INVALID_REQUEST",
message: error.message,
retryable: false,
status: 400,
};
}
if (error instanceof AssetResourceHydrationError) {
return {
code: error.code,
message: error.message,
retryable: false,
details: error.details,
status: 400,
};
}
const visited = new Set<unknown>();
let cause = error;
while (cause !== undefined && visited.has(cause) === false) {
if (cause instanceof DocumentResolutionLimitError) {
const details: Record<string, string | number> = {};
for (const [key, value] of Object.entries({
assetId: cause.documentId,
documentCount: cause.documentCount,
documentLimit: cause.documentLimit,
contentByteLimit: cause.contentByteLimit,
totalBytes: cause.totalBytes,
totalByteLimit: cause.totalByteLimit,
})) {
if (value !== undefined) {
details[key] = value;
}
}
return {
code: "CONTENT_LIMIT_EXCEEDED",
message: cause.message,
retryable: false,
details,
status: 400,
};
}
if (cause instanceof CachedDocumentLoaderError) {
return {
code: "CONTENT_LIMIT_EXCEEDED",
message: cause.message,
retryable: false,
details: {
assetId: cause.documentId,
contentByteLimit: cause.contentByteLimit,
},
status: 400,
};
}
visited.add(cause);
cause = cause instanceof Error ? cause.cause : undefined;
}
};