From 97007196696e379dc5b168e8b8ef3ad89d452ba2 Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Fri, 27 Nov 2026 22:55:19 -0500 Subject: [PATCH 1/2] Import query types instead of copying them Delete the local copies of the types that query modules export, and the 8 casts of fetch* results that hid them. Co-Authored-By: Claude Sonnet 5.5 --- .../components/ai-dashboard/PromptDialogs.tsx | 4 +- .../components/ai-dashboard/PromptsTable.tsx | 9 ++-- .../components/ai-dashboard/RunStatusIcon.tsx | 2 +- .../ai-dashboard/ai-dashboard-model.ts | 40 ++++++---------- app/src/components/ai/LLMSidebar.tsx | 19 +++----- .../editor/ui/PromptsManagerModal.tsx | 18 ++----- app/src/features/umap/UmapRunMetadataCard.tsx | 4 +- app/src/features/umap/detail-types.ts | 28 ++--------- app/src/layout/umap-normalize.ts | 2 +- app/src/routes/ai/index.tsx | 12 ++--- app/src/routes/ai/prompts/[id].tsx | 25 +--------- app/src/routes/ai/runs/[id].tsx | 31 ++---------- app/src/routes/embeddings/[id].tsx | 47 +++---------------- app/src/routes/umap/[id]/index.tsx | 12 ++--- app/src/server/mcp/research-shared.ts | 3 +- app/src/services/docs.resources.ts | 2 +- .../services/research/research.activity.ts | 2 +- app/src/services/umap.service.ts | 3 +- app/src/stores/positions.store.ts | 3 +- app/src/types/notes.ts | 7 --- 20 files changed, 68 insertions(+), 205 deletions(-) diff --git a/app/src/components/ai-dashboard/PromptDialogs.tsx b/app/src/components/ai-dashboard/PromptDialogs.tsx index c47931f7..2c6c4bc7 100644 --- a/app/src/components/ai-dashboard/PromptDialogs.tsx +++ b/app/src/components/ai-dashboard/PromptDialogs.tsx @@ -9,10 +9,10 @@ import { Text } from "~/components/ui/text"; import { Textarea } from "~/components/ui/textarea"; import { deletePrompt, updatePrompt } from "~/services/prompts/prompts.actions"; import { fetchPrompts } from "~/services/prompts/prompts.queries"; -import type { DashboardPrompt } from "./ai-dashboard-model"; +import type { Prompt } from "~/services/prompts/prompts.queries"; type PromptDialogProps = { - prompt: DashboardPrompt | null; + prompt: Prompt | null; onClose: () => void; }; diff --git a/app/src/components/ai-dashboard/PromptsTable.tsx b/app/src/components/ai-dashboard/PromptsTable.tsx index 0ab7e579..5fbcd3d8 100644 --- a/app/src/components/ai-dashboard/PromptsTable.tsx +++ b/app/src/components/ai-dashboard/PromptsTable.tsx @@ -6,13 +6,14 @@ import { IconButton } from "~/components/ui/icon-button"; import * as Table from "~/components/ui/table"; import { Text } from "~/components/ui/text"; import { formatRelativeTimestamp } from "~/features/umap/format"; -import type { DashboardPrompt, PromptRunStats } from "./ai-dashboard-model"; +import type { Prompt } from "~/services/prompts/prompts.queries"; +import type { PromptRunStats } from "./ai-dashboard-model"; type PromptsTableProps = { - prompts: DashboardPrompt[]; + prompts: Prompt[]; stats: Map; - onEdit: (prompt: DashboardPrompt) => void; - onDelete: (prompt: DashboardPrompt) => void; + onEdit: (prompt: Prompt) => void; + onDelete: (prompt: Prompt) => void; }; const stop = (event: MouseEvent) => event.stopPropagation(); diff --git a/app/src/components/ai-dashboard/RunStatusIcon.tsx b/app/src/components/ai-dashboard/RunStatusIcon.tsx index 62f9e533..8ea71820 100644 --- a/app/src/components/ai-dashboard/RunStatusIcon.tsx +++ b/app/src/components/ai-dashboard/RunStatusIcon.tsx @@ -1,7 +1,7 @@ import { AlertTriangleIcon, CheckCircle2Icon, XCircleIcon } from "lucide-solid"; import { Match, Switch } from "solid-js"; import { Box } from "styled-system/jsx"; -import type { RunStatus } from "./ai-dashboard-model"; +import type { RunStatus } from "~/services/ai/ai-runs.queries"; const labels: Record = { SUCCESS: "Succeeded", diff --git a/app/src/components/ai-dashboard/ai-dashboard-model.ts b/app/src/components/ai-dashboard/ai-dashboard-model.ts index 1a980d7d..2c3ad3df 100644 --- a/app/src/components/ai-dashboard/ai-dashboard-model.ts +++ b/app/src/components/ai-dashboard/ai-dashboard-model.ts @@ -1,29 +1,19 @@ -export type RunStatus = "SUCCESS" | "ERROR" | "PARTIAL"; +import type { + PromptRunItem, + RunStatus, +} from "~/services/ai/ai-runs.queries"; -export type DashboardPrompt = { - id: string; - task: string; - description?: string | null; - defaultModel: string; - defaultTemp: number; - defaultTopP?: number | null; - activeVersion?: { - id: string; - template: string; - system?: string | null; - } | null; -}; - -export type DashboardRun = { - id: string; - status: RunStatus; - model: string; - createdAt: string; - error?: string | null; - outputHtml?: string | null; - promptId?: string | null; - promptTask?: string | null; -}; +export type DashboardRun = Pick< + PromptRunItem, + | "id" + | "status" + | "model" + | "createdAt" + | "error" + | "outputHtml" + | "promptId" + | "promptTask" +>; export type PromptRunStats = { count: number; diff --git a/app/src/components/ai/LLMSidebar.tsx b/app/src/components/ai/LLMSidebar.tsx index f61fc603..aede3047 100644 --- a/app/src/components/ai/LLMSidebar.tsx +++ b/app/src/components/ai/LLMSidebar.tsx @@ -30,6 +30,7 @@ import { fetchChatThreads, type ChatThreadDetail, type ChatThreadPreview, + type ChatThreadStatus, } from "~/services/ai/ai-chat.queries"; import { createChatThread, @@ -40,12 +41,6 @@ import { } from "~/services/ai/ai-chat.actions"; import { createKeptAsync, onDemand } from "~/utils/kept-async"; -type ChatThreadStatus = "ACTIVE" | "LOADING" | "DONE"; - -type ThreadPreview = ChatThreadPreview; - -type ThreadDetail = ChatThreadDetail; - const TRANSIENT_SERVER_FUNCTION_ERROR_FRAGMENT = "Malformed server function stream"; @@ -93,18 +88,18 @@ export function useLLMSidebar() { setInitialized(true); }; const threads = createKeptAsync(() => { - if (!initialized()) return Promise.resolve([]); + if (!initialized()) return Promise.resolve([]); return recoverServerFunctionCall( () => fetchChatThreads(), "fetchChatThreads" ); }, onDemand); const thread = createKeptAsync(() => { - if (!initialized()) return Promise.resolve(null); + if (!initialized()) return Promise.resolve(null); const id = selectedId(); - if (!id) return Promise.resolve(null); + if (!id) return Promise.resolve(null); return recoverServerFunctionCall( - () => fetchChatThread(id) as Promise, + () => fetchChatThread(id), "fetchChatThread" ); }, onDemand); @@ -358,10 +353,10 @@ export function useLLMSidebar() { function LLMSidebarView(props: { open: Accessor; onClose: () => void; - threads: Accessor; + threads: Accessor; selectedId: Accessor; onSelect: (id: string) => void; - thread: Accessor; + thread: Accessor; onRefresh: () => void; onCreateThread: (noteId: string, title?: string) => Promise; onSend: (text: string) => Promise; diff --git a/app/src/components/editor/ui/PromptsManagerModal.tsx b/app/src/components/editor/ui/PromptsManagerModal.tsx index 9ff0ea79..4e69a704 100644 --- a/app/src/components/editor/ui/PromptsManagerModal.tsx +++ b/app/src/components/editor/ui/PromptsManagerModal.tsx @@ -9,22 +9,12 @@ import { Textarea } from "~/components/ui/textarea"; import { Text } from "~/components/ui/text"; import { Box, Grid, HStack, Stack } from "styled-system/jsx"; import { SimpleDialog } from "~/components/ui/simple-dialog"; -import { fetchPrompts } from "~/services/prompts/prompts.queries"; +import { + fetchPrompts, + type Prompt, +} from "~/services/prompts/prompts.queries"; import { createPromptVersion, createPrompt, revisePrompt } from "~/services/prompts/prompts.actions"; -type Prompt = { - id: string; - task: string; - description?: string | null; - defaultModel: string; - defaultTemp: number; - defaultTopP?: number | null; - activeVersion?: { - id: string; - template: string; - system?: string | null; - } | null; -}; export function usePromptsManagerModal() { const [open, setOpen] = createSignal(false); const prompts = createKeptAsync(() => fetchPrompts()); diff --git a/app/src/features/umap/UmapRunMetadataCard.tsx b/app/src/features/umap/UmapRunMetadataCard.tsx index 0df30c2f..1394258c 100644 --- a/app/src/features/umap/UmapRunMetadataCard.tsx +++ b/app/src/features/umap/UmapRunMetadataCard.tsx @@ -3,11 +3,11 @@ import { Heading } from "~/components/ui/heading"; import { Link } from "~/components/ui/link"; import { Text } from "~/components/ui/text"; import { formatRelativeTimestamp } from "~/features/umap/format"; -import type { UmapRunMeta } from "~/features/umap/detail-types"; +import type { UmapRunDetail } from "~/services/umap/umap.queries"; import { Box, Grid, Stack } from "styled-system/jsx"; type UmapRunMetadataCardProps = { - meta: UmapRunMeta; + meta: UmapRunDetail; }; export function UmapRunMetadataCard(props: UmapRunMetadataCardProps) { diff --git a/app/src/features/umap/detail-types.ts b/app/src/features/umap/detail-types.ts index 355a1c24..668eaf69 100644 --- a/app/src/features/umap/detail-types.ts +++ b/app/src/features/umap/detail-types.ts @@ -1,28 +1,10 @@ -import type { UmapRegionsSnapshot } from "./region-types"; - -export type UmapPoint = { docId: string; x: number; y: number; z?: number | null }; - -export type UmapRunMeta = { - id: string; - dims: number; - params?: Record | null; - embeddingRunId: string; - createdAt: string; - count?: number; - hasArtifact?: boolean; - artifactPath?: string | null; - regions?: UmapRegionsSnapshot | null; - regionCount?: number; -}; - -export type UmapRunPointsData = { - runId: string; - dims: number; - points: UmapPoint[]; -}; +import type { + UmapPoint, + UmapRunDetail, +} from "~/services/umap/umap.queries"; export type UmapDetailData = { - meta: UmapRunMeta; + meta: UmapRunDetail; dims: number; points: UmapPoint[]; }; diff --git a/app/src/layout/umap-normalize.ts b/app/src/layout/umap-normalize.ts index 259acfba..1455f8c6 100644 --- a/app/src/layout/umap-normalize.ts +++ b/app/src/layout/umap-normalize.ts @@ -1,5 +1,5 @@ import type { UmapRegionsSnapshot } from "~/features/umap/region-types"; -import type { UmapPoint } from "~/types/notes"; +import type { UmapPoint } from "~/services/umap/umap.queries"; type UmapNormalization = { centerX: number; diff --git a/app/src/routes/ai/index.tsx b/app/src/routes/ai/index.tsx index 8379c0c7..a25f5758 100644 --- a/app/src/routes/ai/index.tsx +++ b/app/src/routes/ai/index.tsx @@ -6,8 +6,6 @@ import { PaidCallsTable } from "~/components/ai-dashboard/PaidCallsTable"; import { PromptsTable } from "~/components/ai-dashboard/PromptsTable"; import { RunsTable } from "~/components/ai-dashboard/RunsTable"; import { - type DashboardPrompt, - type DashboardRun, RUN_STATUS_FILTERS, parseRunStatusFilter, runMatchesQuery, @@ -20,16 +18,16 @@ import { Button } from "~/components/ui/button"; import { Input } from "~/components/ui/input"; import { Text } from "~/components/ui/text"; import { fetchPromptRuns } from "~/services/ai/ai-runs.queries"; -import { fetchPrompts } from "~/services/prompts/prompts.queries"; +import { fetchPrompts, type Prompt } from "~/services/prompts/prompts.queries"; const RUN_LIMIT = 100; const AiDashboard: VoidComponent = () => { - const prompts = createAsync(() => fetchPrompts() as Promise); - const runs = createAsync(() => fetchPromptRuns({ limit: RUN_LIMIT }) as Promise); + const prompts = createAsync(() => fetchPrompts()); + const runs = createAsync(() => fetchPromptRuns({ limit: RUN_LIMIT })); const [params, setParams] = useSearchParams(); - const [editing, setEditing] = createSignal(null); - const [deleting, setDeleting] = createSignal(null); + const [editing, setEditing] = createSignal(null); + const [deleting, setDeleting] = createSignal(null); const status = () => parseRunStatusFilter(typeof params.status === "string" ? params.status : undefined); const query = () => (typeof params.q === "string" ? params.q : ""); diff --git a/app/src/routes/ai/prompts/[id].tsx b/app/src/routes/ai/prompts/[id].tsx index 9da75d78..6b8c877d 100644 --- a/app/src/routes/ai/prompts/[id].tsx +++ b/app/src/routes/ai/prompts/[id].tsx @@ -33,27 +33,6 @@ import { updatePrompt, } from "~/services/prompts/prompts.actions"; -type PromptVersion = { - id: string; - template: string; - system?: string | null; - modelOverride?: string | null; - tempOverride?: number | null; - topPOverride?: number | null; - createdAt?: string; -}; - -type PromptFull = { - id: string; - task: string; - description?: string | null; - defaultModel: string; - defaultTemp: number; - defaultTopP?: number | null; - activeVersion?: PromptVersion | null; - versions: PromptVersion[]; -}; - const formatOverrides = (version: { modelOverride?: string | null; tempOverride?: number | null; @@ -85,8 +64,8 @@ export const PromptDetailPage: VoidComponent = () => { const params = useParams(); const navigate = useNavigate(); const prompt = createAsync(() => { - if (!params.id) return Promise.resolve(null); - return fetchPrompt(params.id) as Promise; + if (!params.id) return Promise.resolve(null); + return fetchPrompt(params.id); }); const runs = createAsync(() => { if (!params.id) return Promise.resolve([] as PromptRunItem[]); diff --git a/app/src/routes/ai/runs/[id].tsx b/app/src/routes/ai/runs/[id].tsx index a4f8bfc3..850c6282 100644 --- a/app/src/routes/ai/runs/[id].tsx +++ b/app/src/routes/ai/runs/[id].tsx @@ -2,7 +2,6 @@ import { createAsync, useParams } from "@solidjs/router"; import { type VoidComponent, For, Show, Suspense } from "solid-js"; import { Box, Grid, HStack, Stack, styled } from "styled-system/jsx"; import { RunStatusIcon } from "~/components/ai-dashboard/RunStatusIcon"; -import type { RunStatus } from "~/components/ai-dashboard/ai-dashboard-model"; import { KeyValueList, PlainCodeBlock } from "~/components/secondary/DataBlocks"; import { EmptyState } from "~/components/secondary/EmptyState"; import { SecondaryBreadcrumb } from "~/components/secondary/SecondaryBreadcrumb"; @@ -10,31 +9,7 @@ import { SecondarySection } from "~/components/secondary/SecondarySection"; import { Heading } from "~/components/ui/heading"; import { Text } from "~/components/ui/text"; import { formatRelativeTimestamp } from "~/features/umap/format"; -import { fetchPromptRun } from "~/services/ai/ai-runs.queries"; - -type PromptRunFull = { - id: string; - status: RunStatus; - model: string; - createdAt: string; - error?: string | null; - compiledPrompt: string; - systemUsed?: string | null; - outputHtml?: string | null; - rawResponse?: unknown | null; - inputVars?: Record | null; - promptVersion?: { - id: string; - promptId: string; - Prompt?: { id: string; task: string; defaultModel: string } | null; - } | null; - HumanFeedback?: Array<{ - id: string; - rating?: number | null; - comment?: string | null; - createdAt: string; - }>; -}; +import { fetchPromptRun, type RunStatus } from "~/services/ai/ai-runs.queries"; const statusWord: Record = { SUCCESS: "Succeeded", @@ -45,8 +20,8 @@ const statusWord: Record = { const RunDetailPage: VoidComponent = () => { const params = useParams(); const run = createAsync(() => { - if (!params.id) return Promise.resolve(null); - return fetchPromptRun(params.id) as Promise; + if (!params.id) return Promise.resolve(null); + return fetchPromptRun(params.id); }); return ( diff --git a/app/src/routes/embeddings/[id].tsx b/app/src/routes/embeddings/[id].tsx index df916a1e..968f4eab 100644 --- a/app/src/routes/embeddings/[id].tsx +++ b/app/src/routes/embeddings/[id].tsx @@ -26,6 +26,7 @@ import { Box, Container, Flex, Grid, HStack, Stack } from "styled-system/jsx"; import { fetchDocSections, fetchEmbeddingRun, + type DocSectionItem, } from "~/services/embeddings/embeddings.queries"; import { deleteEmbeddingRun, @@ -36,42 +37,8 @@ import { fetchUmapRuns } from "~/services/umap/umap.queries"; import { fetchJobStatus, type JobStatus } from "~/services/jobs/jobs.queries"; import { createKeptAsync } from "~/utils/kept-async"; -type EmbeddingRun = { - id: string; - model: string; - dims: number; - params: Record | null; - createdAt: string; - count: number; - sectionCount?: number; - remaining?: number; - changedEligible?: number; - docs?: { - items: { id: string; title: string; embeddedAt: string }[]; - limit: number; - offset: number; - total: number; - }; -}; - const DOCS_LIMIT = 10; -type SectionItem = { - id: string; - headingPath: string[]; - orderIndex: number; - charCount: number; - preview: string; - embedded?: boolean; -}; - -type UmapRunSummary = { - id: string; - dims: number; - hasArtifact: boolean; - createdAt: string; -}; - function formatParamValue(value: unknown): string { if (value == null) return "null"; if (typeof value === "string") return value; @@ -97,20 +64,20 @@ const EmbeddingDetail: VoidComponent = () => { const [docsOffset, setDocsOffset] = createSignal(0); const [titleQuery, setTitleQuery] = createSignal(""); const run = createAsync(() => { - if (!params.id) return Promise.resolve(null); + if (!params.id) return Promise.resolve(null); return fetchEmbeddingRun({ id: params.id, includeDocs: true, limit: DOCS_LIMIT, offset: docsOffset(), titleQuery: titleQuery(), - }) as Promise; + }); }); const [state, setState] = createStore({ dims: 2 as 2 | 3, batchSize: 200, selectedDocId: null as string | null, - sections: [] as SectionItem[], + sections: [] as DocSectionItem[], sectionsLoading: false, activeJobId: "", lastProjectionSummary: null as null | { @@ -119,11 +86,9 @@ const EmbeddingDetail: VoidComponent = () => { failedRuns: number; }, }); - const umapRunsForEmbedding = createAsync((): Promise => { + const umapRunsForEmbedding = createAsync(() => { if (!params.id) return Promise.resolve([]); - return fetchUmapRuns({ embeddingRunId: params.id, limit: 200 }) as Promise< - UmapRunSummary[] - >; + return fetchUmapRuns({ embeddingRunId: params.id, limit: 200 }); }); const activeJob = createKeptAsync((): Promise => { if (!state.activeJobId) return Promise.resolve(null); diff --git a/app/src/routes/umap/[id]/index.tsx b/app/src/routes/umap/[id]/index.tsx index 73ddb1e7..401facc0 100644 --- a/app/src/routes/umap/[id]/index.tsx +++ b/app/src/routes/umap/[id]/index.tsx @@ -18,11 +18,7 @@ import { regenerateUmapRegionsAction, } from "~/services/umap/umap.actions"; import { getParamEntries } from "~/features/umap/format"; -import { - type UmapDetailData, - type UmapRunMeta, - type UmapRunPointsData, -} from "~/features/umap/detail-types"; +import { type UmapDetailData } from "~/features/umap/detail-types"; import { useUmapCanvas } from "~/features/umap/useUmapCanvas"; import { UmapRunCanvasCard } from "~/features/umap/UmapRunCanvasCard"; import { UmapRunMetadataCard } from "~/features/umap/UmapRunMetadataCard"; @@ -40,12 +36,10 @@ const UmapDetail: VoidComponent = () => { const detail = createAsync(async (): Promise => { if (!params.id) return null; - const run = (await fetchUmapRun(params.id)) as UmapRunMeta | null; + const run = await fetchUmapRun(params.id); if (!run) return null; - const pointsData = (await fetchUmapPointsForRun(params.id).catch( - () => null, - )) as UmapRunPointsData | null; + const pointsData = await fetchUmapPointsForRun(params.id).catch(() => null); return { meta: run, diff --git a/app/src/server/mcp/research-shared.ts b/app/src/server/mcp/research-shared.ts index a794e3be..1c600fb6 100644 --- a/app/src/server/mcp/research-shared.ts +++ b/app/src/server/mcp/research-shared.ts @@ -3,6 +3,7 @@ import { z } from "zod"; import { batchPayload, logResearchEvent, + type Topic, } from "~/services/research/research.activity"; import { prisma } from "~/server/db"; import type { IngestResult } from "~/services/research/research.ingest"; @@ -20,8 +21,6 @@ import { // What the research tool files share: argument shapes, topic lookup, and the // one path every card write takes into an undoable, logged batch. -export type Topic = { id: string; slug: string; title: string }; - export const topicArg = z .string() .min(1) diff --git a/app/src/services/docs.resources.ts b/app/src/services/docs.resources.ts index 68555683..a71d6b8f 100644 --- a/app/src/services/docs.resources.ts +++ b/app/src/services/docs.resources.ts @@ -5,7 +5,7 @@ import { fetchLatestUmapRun, fetchUmapPoints, } from "~/services/umap.service"; -import type { UmapPoint } from "~/types/notes"; +import type { UmapPoint } from "~/services/umap/umap.queries"; import { createKeptAsync } from "~/utils/kept-async"; export function useDocsResource() { diff --git a/app/src/services/research/research.activity.ts b/app/src/services/research/research.activity.ts index e9111343..37eefdf2 100644 --- a/app/src/services/research/research.activity.ts +++ b/app/src/services/research/research.activity.ts @@ -12,7 +12,7 @@ export type ResearchEventType = | "research.batch_undone" | "research.batch_redone"; -type Topic = { id: string; slug: string; title: string }; +export type Topic = { id: string; slug: string; title: string }; export const logResearchEvent = ( eventType: ResearchEventType, diff --git a/app/src/services/umap.service.ts b/app/src/services/umap.service.ts index ae8b0f6e..5425169b 100644 --- a/app/src/services/umap.service.ts +++ b/app/src/services/umap.service.ts @@ -1,6 +1,7 @@ import { query } from "@solidjs/router"; import { prisma } from "~/server/db"; -import type { UmapPoint, UmapRun } from "~/types/notes"; +import type { UmapRun } from "~/types/notes"; +import type { UmapPoint } from "~/services/umap/umap.queries"; import { parseUmapRegionsSnapshot } from "~/services/umap/umap.regions"; export type NearbyUmapDoc = { diff --git a/app/src/stores/positions.store.ts b/app/src/stores/positions.store.ts index d3336a5b..3fab5107 100644 --- a/app/src/stores/positions.store.ts +++ b/app/src/stores/positions.store.ts @@ -1,5 +1,6 @@ import { createMemo, createSignal, type Accessor } from "solid-js"; -import type { DocItem, UmapPoint, UmapRun } from "~/types/notes"; +import type { DocItem, UmapRun } from "~/types/notes"; +import type { UmapPoint } from "~/services/umap/umap.queries"; import { normalizeUmap } from "~/layout/umap-normalize"; import { seededPositionFor } from "~/layout/seeded"; import { placeApproximate } from "~/layout/approximate-placement"; diff --git a/app/src/types/notes.ts b/app/src/types/notes.ts index 95a6c153..f0471f53 100644 --- a/app/src/types/notes.ts +++ b/app/src/types/notes.ts @@ -13,13 +13,6 @@ export type DocItem = { } | null; }; -export type UmapPoint = { - docId: string; - x: number; - y: number; - z?: number | null; -}; - export type UmapRun = { id: string; dims: number; From 87fbd5314c3008698b6c37d4ab0475eec9bc6258 Mon Sep 17 00:00:00 2001 From: Byron Wall Date: Fri, 27 Nov 2026 22:55:19 -0500 Subject: [PATCH 2/2] Lint against copies of query types Add vn/no-query-type-redeclare and drop the six exceptions from vn/no-query-result-cast. Co-Authored-By: Claude Sonnet 5.5 --- app/.oxlintrc.json | 16 ++----------- app/lint/oxlint-plugin-vn.mjs | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/app/.oxlintrc.json b/app/.oxlintrc.json index 621c2dc6..95bfcba9 100644 --- a/app/.oxlintrc.json +++ b/app/.oxlintrc.json @@ -81,7 +81,8 @@ "vn/known-color-steps": "error", "vn/resource-read-latest": "error", "vn/row-columns": "error", - "vn/no-query-result-cast": "error" + "vn/no-query-result-cast": "error", + "vn/no-query-type-redeclare": "error" }, "overrides": [ { @@ -100,19 +101,6 @@ "files": ["**/*.test.ts"], "rules": { "vn/no-router-actions-in-server": "off" } }, - { - // Casts left to clean up. Remove a file once its local copy of the - // query's type is gone. - "files": [ - "src/components/ai/LLMSidebar.tsx", - "src/routes/embeddings/[[]id].tsx", - "src/routes/umap/[[]id]/index.tsx", - "src/routes/ai/prompts/[[]id].tsx", - "src/routes/ai/index.tsx", - "src/routes/ai/runs/[[]id].tsx" - ], - "rules": { "vn/no-query-result-cast": "off" } - }, { "files": ["src/lib/collections/colors.ts"], "rules": { "vn/color-tokens": "off" } diff --git a/app/lint/oxlint-plugin-vn.mjs b/app/lint/oxlint-plugin-vn.mjs index 41c28ccb..beda5873 100644 --- a/app/lint/oxlint-plugin-vn.mjs +++ b/app/lint/oxlint-plugin-vn.mjs @@ -1,5 +1,8 @@ // Repo-specific guard rules for oxlint, loaded through `jsPlugins` in // .oxlintrc.json. Each rule reports every node that matches its selector. +import { readdirSync, readFileSync } from "node:fs"; +import path from "node:path"; + const selectorRule = (selector, message) => ({ create(context) { return { @@ -163,6 +166,46 @@ const rowColumns = { }, }; +// Type names that a `*.queries.ts` module exports, with that module's path. +// Read once per lint run. +const queryTypes = () => { + const src = path.join(import.meta.dirname, "../src"); + const names = new Map(); + for (const file of readdirSync(src, { recursive: true })) { + if (!file.endsWith(".queries.ts")) continue; + const text = readFileSync(path.join(src, file), "utf8"); + for (const [, name] of text.matchAll( + /^export (?:type|interface) (\w+)/gm, + )) { + names.set(name, `~/${file.slice(0, -3)}`); + } + } + return names; +}; + +let queryTypeNames; + +// A second declaration of a query's type compiles for as long as its fields +// stay assignable, so it drifts without an error. +const noQueryTypeRedeclare = { + create(context) { + if (context.filename.endsWith(".queries.ts")) return {}; + queryTypeNames ??= queryTypes(); + const check = ({ id }) => { + const from = queryTypeNames.get(id.name); + if (!from) return; + context.report({ + node: id, + message: `\`${id.name}\` is a query module's type. Import it from ${from}, or give a different type a different name (derive it with Pick or Omit).`, + }); + }; + return { + TSTypeAliasDeclaration: check, + TSInterfaceDeclaration: check, + }; + }, +}; + export default { meta: { name: "vn" }, rules: { @@ -184,6 +227,7 @@ export default { ":matches(Property[key.name=/^(color|bg|background|backgroundColor|borderColor|outlineColor|fill|stroke|stopColor)$/], JSXAttribute[name.name=/^(color|bg|background|backgroundColor|borderColor|outlineColor|fill|stroke|stopColor)$/]) > Literal[value=/^#[0-9a-fA-F]{3,8}$/]", "Use a color token (fg.*, bg.*, border.*, or a palette step) instead of a raw hex color, so light and dark mode and the palette stay in step.", ), + "no-query-type-redeclare": noQueryTypeRedeclare, "no-query-result-cast": selectorRule( "TSAsExpression:matches([expression.callee.name=/^fetch[A-Z]/], [expression.argument.callee.name=/^fetch[A-Z]/])", "Do not cast a fetch* query's result. Its return type is the contract: import the type from the query module, or derive it with Awaited>, instead of copying it here.",