-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimportCadKnowledgeCsv.js
More file actions
85 lines (77 loc) · 3.12 KB
/
Copy pathimportCadKnowledgeCsv.js
File metadata and controls
85 lines (77 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import "dotenv/config";
import { createClient } from "@supabase/supabase-js";
import {
loadSeedEntriesFromCsv,
toCadKnowledgeRecord,
toCadMemoryRecord,
} from "./lib/cadSeedData.js";
// THIS IS USED TO IMPORT DATA, CAN PROBABLY
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_ANON_KEY;
if (!supabaseUrl || !supabaseKey) {
throw new Error("SUPABASE_URL and SUPABASE_ANON_KEY must be set to import CSV data.");
}
const supabase = createClient(supabaseUrl, supabaseKey, {
auth: { persistSession: false, autoRefreshToken: false },
});
async function upsert(table, records, options) {
const { error } = await supabase.from(table).upsert(records, options);
if (error) {
if (error.code === "42P10") {
console.warn(`[Import] ${table} is missing the unique constraint needed for onConflict=${options?.onConflict || "(none)"}; inserting records whose titles are not present.`);
const titles = records.map(record => record.title).filter(Boolean);
const existing = titles.length
? await supabase.from(table).select("title").in("title", titles)
: { data: [], error: null };
if (existing.error) throw existing.error;
const seen = new Set((existing.data || []).map(row => row.title));
const missing = records.filter(record => !record.title || !seen.has(record.title));
if (!missing.length) {
console.log(`[Import] No new ${table} records to insert.`);
return;
}
const inserted = await supabase.from(table).insert(missing);
if (inserted.error) {
if (inserted.error.code === "42501") {
console.warn(`[Import] Skipped ${table} insert fallback because row-level security rejected anon inserts. Local CSV retrieval still works.`);
return;
}
throw inserted.error;
}
console.log(`[Import] Inserted ${missing.length} records into ${table}.`);
return;
}
throw error;
}
console.log(`[Import] Upserted ${records.length} records into ${table}.`);
}
async function run() {
const filePath = process.argv[2] || "./data/cadKnowledge.csv";
const isPruning = /pruning/i.test(filePath);
const isMemoryExamples = /memory|example/i.test(filePath);
const sourceTable = isPruning ? "pruning_table" : isMemoryExamples ? "cad_memory" : "cad_knowledge";
const defaultMemoryType = isPruning ? "pruning_rule" : isMemoryExamples ? "example" : "seed";
const entries = loadSeedEntriesFromCsv(filePath, {
sourceTable,
memoryType: defaultMemoryType,
memoryOnly: sourceTable === "pruning_table" || sourceTable === "cad_memory",
});
if (!entries.length) {
console.log("No valid rows found in CSV.");
return;
}
const cadKnowledgeRecords = entries
.map(toCadKnowledgeRecord)
.filter(Boolean);
const memoryRecords = entries
.map(toCadMemoryRecord)
.filter(Boolean);
if (cadKnowledgeRecords.length) {
await upsert("cad_knowledge", cadKnowledgeRecords, { onConflict: "title" });
}
await upsert("cad_memory", memoryRecords, { onConflict: "title" });
}
run().catch(err => {
console.error("[Import] Failed:", err.message || err);
process.exit(1);
});