-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathsetDirectUploadConnection.ts
More file actions
176 lines (157 loc) · 5.12 KB
/
setDirectUploadConnection.ts
File metadata and controls
176 lines (157 loc) · 5.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { databaseDrizzle } from "@/db";
import { connections, processedFiles } from "@/db/schema";
import { tryAndCatch } from "@/lib/try-catch";
import { qdrant_collection_name, qdrantClient } from "@/qdrant";
import { and, eq } from "drizzle-orm";
import { z } from "zod";
const updateDirectUploadConfig = z.object({
userId: z.string().min(5),
connectionId: z.string().min(5),
pageLimit: z.string().nullable().transform((str, ctx): number | null => {
try {
if (str) return parseInt(str)
return null
} catch (error) {
ctx.addIssue({ code: 'custom', message: "invalid page limit" })
return z.NEVER
}
}),
identifier: z.string().min(2).optional(),
metadata: z.string().optional()
.transform((str, ctx): string | undefined => {
try {
if (str) {
JSON.parse(str)
return str
}
} catch (e) {
ctx.addIssue({ code: 'custom', message: 'Invalid JSON' })
return z.NEVER
}
}),
files: z.array(z.any().refine((file) => {
return (
file ||
(file instanceof File && file.type === "application/pdf")
);
},
{
message: "Invalid File",
})
),
links: z.array(z.string().min(5)),
texts: z.array(z.string().min(5)),
removedFiles: z.array(z.string().min(5))
})
export const updateDirectUploadConnection = async (formData: FormData) => {
const config = updateDirectUploadConfig.safeParse({
userId: formData.get("userId"),
connectionId: formData.get("connectionId"),
identifier: formData.get("identifier"),
metadata: formData.get("metadata") || "{}",
files: formData.getAll("files") || [],
links: formData.getAll("links") || [],
texts: formData.getAll("texts") || [],
removedFiles: formData.getAll("removedFiles") || [],
pageLimit: formData.get("pageLimit")
})
if (!config.success) {
throw new Error(`Validation errors - ${config.error.message}`)
}
const connectionChunksIds: { chunksIds: string[], name: string }[] = [];
await databaseDrizzle
.update(connections)
.set({
metadata: config.data.metadata,
identifier: config.data.identifier
}).where(eq(connections.id, config.data.connectionId))
for (const fileName of config.data.removedFiles) {
const files = await databaseDrizzle
.delete(processedFiles)
.where(and(
eq(processedFiles.connectionId, config.data.connectionId),
eq(processedFiles.name, fileName)
)).returning({ chunksIds: processedFiles.chunksIds, name: processedFiles.name })
connectionChunksIds.push(...files)
}
for (const { chunksIds, name } of connectionChunksIds) {
await tryAndCatch(qdrantClient.delete(qdrant_collection_name, {
points: chunksIds,
filter: {
must: [{ "key": "_document_id", "match": { value: name } },
{ key: "_userId", match: { value: config.data.userId } }]
}
}))
}
const files = config.data.files.map(async (file) => ({
name: file.name,
size: file.size,
type: file.type,
lastModified: file.lastModified,
content: await fileToBase64(file),
}))
return {
connectionId: config.data.connectionId,
service: "DIRECT_UPLOAD",
metadata: config.data.metadata ?? "{}",
files: await Promise.all(files),
links: config.data.links,
texts: config.data.texts,
pageLimit: config.data.pageLimit,
fileLimit: null
}
}
export const setDirectUploadConnection = async (formData: FormData) => {
const config = directUploadConfig.safeParse({
userId: formData.get("userId"),
identifier: formData.get("identifier"),
metadata: formData.get("metadata") || "{}",
pageLimit: formData.get("pageLimit"),
fileLimit: formData.get("fileLimit"),
files: formData.getAll("files"),
links: formData.getAll("links"),
texts: formData.getAll("texts")
})
if (!config.success) {
throw new Error(`Validation errors - ${config.error.message}`)
}
const { files, links, userId, identifier, metadata, fileLimit, pageLimit, texts } = config.data;
if (files.length === 0 && links.length === 0 && texts.length === 0) {
throw new Error('Please provide at least one file or link to proceed.')
}
const conn = await databaseDrizzle.insert(connections).values({
userId: userId,
identifier: identifier,
service: 'DIRECT_UPLOAD',
metadata: metadata,
isConfigSet: true,
limitPages: pageLimit,
limitFiles: fileLimit,
}).returning({ id: connections.id })
const allFiles = files.map(async (file) => ({
name: file.name,
size: file.size,
type: file.type,
lastModified: file.lastModified,
content: await fileToBase64(file),
}))
return {
connectionId: conn[0].id,
service: "DIRECT_UPLOAD",
metadata: metadata,
files: await Promise.all(allFiles),
links: links,
texts: texts,
pageLimit: config.data.pageLimit,
fileLimit: fileLimit
}
}
const fileToBase64 = async (file: File): Promise<string> => {
const arrayBuffer = await file.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
let binary = '';
for (let i = 0; i < uint8Array.length; i++) {
binary += String.fromCharCode(uint8Array[i]);
}
return btoa(binary);
};