Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/backfill-smart-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ async function main() {

const enrichment = await upsertDocumentEnrichment({
supabase,
document,
document: document as Parameters<typeof upsertDocumentEnrichment>[0]["document"],
chunks: chunks as never,
images: images as never,
});
Expand All @@ -613,7 +613,7 @@ async function main() {
});
const memory = await upsertDocumentDeepMemory({
supabase,
document,
document: document as Parameters<typeof upsertDocumentDeepMemory>[0]["document"],
chunks: chunks as never,
images: images as never,
summary: enrichment.summary.summary,
Expand Down
6 changes: 5 additions & 1 deletion scripts/backfill-source-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { loadEnvConfig } from "@next/env";
import type { Json } from "@/lib/supabase/database.types";

loadEnvConfig(process.cwd());

Expand Down Expand Up @@ -658,7 +659,10 @@ async function main() {
if (!APPLY) return;

for (const item of changed) {
const { error } = await supabase.from("documents").update({ metadata: item.metadata }).eq("id", item.document.id);
const { error } = await supabase
.from("documents")
.update({ metadata: item.metadata as Json })
.eq("id", item.document.id);
if (error) throw new Error(`Failed to update ${item.document.file_name}: ${error.message}`);
}
console.log(`Applied source metadata backfill to ${changed.length} documents.`);
Expand Down
10 changes: 8 additions & 2 deletions scripts/backfill-visual-intelligence.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { loadEnvConfig } from "@next/env";
import type { Json, TablesInsert } from "@/lib/supabase/database.types";
import {
buildVisualDocumentIndexUnitInputs,
embeddingTextForDocumentIndexUnit,
Expand Down Expand Up @@ -241,7 +242,10 @@ async function loadChunks(documentId: string) {

async function markImage(image: BackfillImageRow, patch: Record<string, unknown>) {
const metadata = { ...(image.metadata ?? {}), ...patch };
const { error } = await supabase.from("document_images").update({ metadata }).eq("id", image.id);
const { error } = await supabase
.from("document_images")
.update({ metadata: metadata as Json })
.eq("id", image.id);
if (error) throw new Error(error.message);
}

Expand Down Expand Up @@ -286,7 +290,9 @@ async function backfillDocument(documentId: string, images: BackfillImageRow[])
...unit,
embedding: embeddings[start + index],
}));
const { error } = await supabase.from("document_index_units").insert(batch);
const { error } = await supabase
.from("document_index_units")
.insert(batch as unknown as TablesInsert<"document_index_units">[]);
if (error) throw new Error(error.message);
}
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/cleanup-abandoned-reindex-generations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function main() {
assertSupabaseHealthy(await probeSupabaseHealth(supabase), "Abandoned reindex generation cleanup");

const { data, error } = await supabase.rpc("cleanup_abandoned_document_index_generations", {
p_document_id: args.documentId,
p_document_id: args.documentId ?? undefined,
p_limit: limit,
p_dry_run: true,
});
Expand Down Expand Up @@ -93,7 +93,7 @@ async function main() {
}

const applied = await supabase.rpc("cleanup_abandoned_document_index_generations", {
p_document_id: args.documentId,
p_document_id: args.documentId ?? undefined,
p_limit: limit,
p_dry_run: false,
});
Expand Down
13 changes: 8 additions & 5 deletions scripts/enrich-documents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { loadEnvConfig } from "@next/env";
import type { SupabaseClient } from "@supabase/supabase-js";
import type { Json } from "@/lib/supabase/database.types";
import { createHash } from "node:crypto";

loadEnvConfig(process.cwd());
Expand Down Expand Up @@ -134,7 +136,8 @@ async function loadRowsForDocuments(supabase: SupabaseAdmin, table: string, sele
for (let start = 0; start < documentIds.length; start += 5) {
const ids = documentIds.slice(start, start + 5);
for (let rangeStart = 0; ; rangeStart += 1000) {
const { data, error } = await supabase
// Dynamic table/select strings need the untyped client surface.
const { data, error } = await (supabase as unknown as SupabaseClient)
.from(table)
.select(select)
.in("document_id", ids)
Expand Down Expand Up @@ -394,7 +397,7 @@ async function classifyExistingImages(supabase: SupabaseAdmin, documentId: strin
clinical_use_reason: finalAssessment.clinical_use_reason,
clinical_signal_score: finalAssessment.clinical_signal_score,
admin_signal_score: finalAssessment.admin_signal_score,
});
} as Parameters<typeof classifiedImageSkipReason>[0]);
const retainAsAuditTable =
image.source_kind === "table_crop" &&
["administrative", "reference"].includes(finalAssessment.clinical_use_class) &&
Expand Down Expand Up @@ -570,7 +573,7 @@ async function main() {
.from("documents")
.update({
image_count: imageStats.searchable,
metadata: imageMetadata,
metadata: imageMetadata as Json,
})
.eq("id", document.id);
}
Expand Down Expand Up @@ -603,8 +606,8 @@ async function main() {
const deepMemory = await upsertDocumentDeepMemory({
supabase,
document: { ...document, metadata: imageMetadata },
chunks: evidence.chunks,
images: evidence.images,
chunks: evidence.chunks as unknown as Parameters<typeof upsertDocumentDeepMemory>[0]["chunks"],
images: evidence.images as unknown as Parameters<typeof upsertDocumentDeepMemory>[0]["images"],
summary: enrichmentSummary,
});
const { data: latestDoc } = await supabase
Expand Down
4 changes: 2 additions & 2 deletions scripts/profile-retrieval-rpcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ async function main() {
p_rpc: rpcName,
p_query_text: args.query,
p_match_count: args.matchCount,
p_owner_filter: ownerId ?? null,
p_document_filters: args.documentIds ?? null,
p_owner_filter: ownerId ?? undefined,
p_document_filters: args.documentIds ?? undefined,
p_analyze: args.analyze,
});
if (error) {
Expand Down
5 changes: 4 additions & 1 deletion scripts/promote-query-misses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ async function main() {

let inserted = 0;
for (const group of promotable) {
const labels = group.labels.filter((label) => label.document_id && label.label && label.label_type);
const labels = group.labels.filter(
(label): label is typeof label & { document_id: string; label: string; label_type: string } =>
Boolean(label.document_id && label.label && label.label_type),
);
for (const label of labels) {
const { error: labelError } = await supabase.from("document_labels").upsert(
{
Expand Down
11 changes: 8 additions & 3 deletions src/app/api/documents/[id]/reindex/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import type { SupabaseClient } from "@supabase/supabase-js";
import { z } from "zod";
import { env, isDemoMode } from "@/lib/env";
import { upsertDocumentEnrichment } from "@/lib/document-enrichment";
Expand Down Expand Up @@ -74,7 +75,11 @@ async function selectReindexRowsInPages<T>(args: {
}) {
const rows: T[] = [];
for (let offset = 0; ; offset += reindexPageSize) {
let query = args.supabase.from(args.table).select(args.select).eq("document_id", args.documentId);
// Dynamic table/select strings need the untyped client surface.
let query = (args.supabase as unknown as SupabaseClient)
.from(args.table)
.select(args.select)
.eq("document_id", args.documentId);
if (args.searchableOnly) query = query.eq("searchable", true);
const { data, error } = await query.range(offset, offset + reindexPageSize - 1);
if (error) throw new Error(error.message);
Expand Down Expand Up @@ -147,13 +152,13 @@ export async function POST(request: Request, { params }: { params: Promise<{ id:

const enrichment = await upsertDocumentEnrichment({
supabase,
document,
document: document as Parameters<typeof upsertDocumentEnrichment>[0]["document"],
chunks: committedChunks,
images: committedImages,
});
const deepMemory = await upsertDocumentDeepMemory({
supabase,
document,
document: document as Parameters<typeof upsertDocumentDeepMemory>[0]["document"],
chunks: committedChunks,
images: committedImages,
summary: enrichment.summary.summary,
Expand Down
4 changes: 3 additions & 1 deletion src/app/api/documents/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import type { Json } from "@/lib/supabase/database.types";
import { z } from "zod";
import { getDemoDocumentPayload } from "@/lib/demo-data";
import { env, isDemoMode } from "@/lib/env";
Expand Down Expand Up @@ -451,7 +452,8 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
renamed_at: new Date().toISOString(),
previous_title: document.title,
original_file_name: metadata.original_file_name ?? document.file_name,
},
// JSON-serializable; the inferred literal type is wider than Json.
} as unknown as Json,
})
.eq("id", id)
.eq("owner_id", user.id)
Expand Down
11 changes: 8 additions & 3 deletions src/app/api/documents/bulk/reindex/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import type { SupabaseClient } from "@supabase/supabase-js";
import { z } from "zod";
import { upsertDocumentDeepMemory } from "@/lib/deep-memory";
import { upsertDocumentEnrichment } from "@/lib/document-enrichment";
Expand Down Expand Up @@ -66,7 +67,11 @@ async function selectRowsInPages<T>(args: {
}) {
const rows: T[] = [];
for (let offset = 0; ; offset += pageSize) {
let query = args.supabase.from(args.table).select(args.select).eq("document_id", args.documentId);
// Dynamic table/select strings need the untyped client surface.
let query = (args.supabase as unknown as SupabaseClient)
.from(args.table)
.select(args.select)
.eq("document_id", args.documentId);
if (args.searchableOnly) query = query.eq("searchable", true);
const { data, error } = await query.range(offset, offset + pageSize - 1);
if (error) throw new Error(error.message);
Expand Down Expand Up @@ -145,13 +150,13 @@ export async function POST(request: Request) {
);
const enrichment = await upsertDocumentEnrichment({
supabase,
document,
document: document as Parameters<typeof upsertDocumentEnrichment>[0]["document"],
chunks: committedChunks,
images: committedImages,
});
const memory = await upsertDocumentDeepMemory({
supabase,
document,
document: document as Parameters<typeof upsertDocumentDeepMemory>[0]["document"],
chunks: committedChunks,
images: committedImages,
summary: enrichment.summary.summary,
Expand Down
3 changes: 2 additions & 1 deletion src/app/api/documents/bulk/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isDemoMode } from "@/lib/env";
import { jsonError, PublicApiError } from "@/lib/http";
import { invalidateRagCachesForOwner } from "@/lib/rag";
import { createAdminClient } from "@/lib/supabase/admin";
import type { Json, TablesUpdate } from "@/lib/supabase/database.types";
import { AuthenticationError, requireAuthenticatedUser, unauthorizedResponse } from "@/lib/supabase/auth";
import { parseJsonBody } from "@/lib/validation/body";

Expand Down Expand Up @@ -156,7 +157,7 @@ export async function POST(request: Request) {
metadata.bulk_metadata_updated_by = user.id;

const nextTitle = editTitle(document.title, parsed.titleEdit);
const updatePayload: Record<string, unknown> = { metadata };
const updatePayload: TablesUpdate<"documents"> = { metadata: metadata as Json };
if (nextTitle && nextTitle !== document.title) updatePayload.title = nextTitle;

const { error: updateError } = await supabase
Expand Down
6 changes: 4 additions & 2 deletions src/app/api/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextResponse } from "next/server";
import type { Json } from "@/lib/supabase/database.types";
import { z } from "zod";
import { demoSearch } from "@/lib/demo-data";
import { isDemoMode, isLocalNoAuthMode } from "@/lib/env";
Expand Down Expand Up @@ -539,7 +540,8 @@ function logRetrievalDiagnostics(args: {
relevance_score: args.relevance.score,
latency_bucket: latencyBucket(latencyMs),
...retrievalDecisionTelemetry(args.telemetry),
},
// Telemetry values are JSON-serializable; some are typed wider than Json.
} as unknown as Json,
});
} catch (error) {
retrievalLogWriteMetrics.failures += 1;
Expand Down Expand Up @@ -599,7 +601,7 @@ function logSearchObservation(args: {
search_cache_hit: telemetry.search_cache_hit ?? null,
embedding_skipped: telemetry.embedding_skipped ?? null,
...retrievalDecisionTelemetry(telemetry),
},
} as unknown as Json,
});
} catch {
// Search telemetry must not affect the user-facing search path.
Expand Down
Loading
Loading