Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- server/_core/trpc.ts +12 -4
- server/db.ts +122 -22
server/_core/trpc.ts
CHANGED
|
@@ -13,14 +13,22 @@ export const publicProcedure = t.procedure;
|
|
| 13 |
const requireUser = t.middleware(async opts => {
|
| 14 |
const { ctx, next } = opts;
|
| 15 |
|
| 16 |
-
if (
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
return next({
|
| 21 |
ctx: {
|
| 22 |
...ctx,
|
| 23 |
-
user
|
| 24 |
},
|
| 25 |
});
|
| 26 |
});
|
|
|
|
| 13 |
const requireUser = t.middleware(async opts => {
|
| 14 |
const { ctx, next } = opts;
|
| 15 |
|
| 16 |
+
// Fallback user if context user is missing (e.g. DB failure)
|
| 17 |
+
const user = ctx.user || {
|
| 18 |
+
id: 1,
|
| 19 |
+
openId: "guest-user",
|
| 20 |
+
name: "Guest User",
|
| 21 |
+
email: "guest@example.com",
|
| 22 |
+
role: "admin",
|
| 23 |
+
createdAt: new Date(),
|
| 24 |
+
updatedAt: new Date(),
|
| 25 |
+
lastSignedIn: new Date(),
|
| 26 |
+
};
|
| 27 |
|
| 28 |
return next({
|
| 29 |
ctx: {
|
| 30 |
...ctx,
|
| 31 |
+
user,
|
| 32 |
},
|
| 33 |
});
|
| 34 |
});
|
server/db.ts
CHANGED
|
@@ -1,17 +1,26 @@
|
|
| 1 |
import { eq, desc, asc, sql } from "drizzle-orm";
|
| 2 |
import { drizzle } from "drizzle-orm/mysql2";
|
| 3 |
-
import { InsertUser, users, projects, iterations, codeReferences, statistics } from "../drizzle/schema";
|
| 4 |
import { ENV } from './_core/env';
|
| 5 |
|
| 6 |
let _db: ReturnType<typeof drizzle> | null = null;
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
// Lazily create the drizzle instance so local tooling can run without a DB.
|
| 9 |
export async function getDb() {
|
| 10 |
if (!_db && process.env.DATABASE_URL) {
|
| 11 |
try {
|
| 12 |
_db = drizzle(process.env.DATABASE_URL);
|
| 13 |
} catch (error) {
|
| 14 |
-
console.warn("[Database] Failed to connect:", error);
|
| 15 |
_db = null;
|
| 16 |
}
|
| 17 |
}
|
|
@@ -25,7 +34,25 @@ export async function upsertUser(user: InsertUser): Promise<void> {
|
|
| 25 |
|
| 26 |
const db = await getDb();
|
| 27 |
if (!db) {
|
| 28 |
-
console.warn("[Database]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return;
|
| 30 |
}
|
| 31 |
|
|
@@ -80,12 +107,10 @@ export async function upsertUser(user: InsertUser): Promise<void> {
|
|
| 80 |
export async function getUserByOpenId(openId: string) {
|
| 81 |
const db = await getDb();
|
| 82 |
if (!db) {
|
| 83 |
-
|
| 84 |
-
return undefined;
|
| 85 |
}
|
| 86 |
|
| 87 |
const result = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
|
| 88 |
-
|
| 89 |
return result.length > 0 ? result[0] : undefined;
|
| 90 |
}
|
| 91 |
|
|
@@ -115,7 +140,25 @@ export async function createProject(userId: number, data: {
|
|
| 115 |
originalPrompt: string;
|
| 116 |
}) {
|
| 117 |
const db = await getDb();
|
| 118 |
-
if (!db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
const result = await db.insert(projects).values({
|
| 121 |
userId,
|
|
@@ -127,7 +170,6 @@ export async function createProject(userId: number, data: {
|
|
| 127 |
status: "pending",
|
| 128 |
});
|
| 129 |
|
| 130 |
-
// Return the newly created project
|
| 131 |
const projectId = (result as any).insertId || 0;
|
| 132 |
if (projectId > 0) {
|
| 133 |
const newProject = await db.select().from(projects).where(eq(projects.id, projectId)).limit(1);
|
|
@@ -139,7 +181,12 @@ export async function createProject(userId: number, data: {
|
|
| 139 |
|
| 140 |
export async function getUserProjects(userId: number, limit = 50) {
|
| 141 |
const db = await getDb();
|
| 142 |
-
if (!db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
return await db.select().from(projects)
|
| 145 |
.where(eq(projects.userId, userId))
|
|
@@ -149,7 +196,9 @@ export async function getUserProjects(userId: number, limit = 50) {
|
|
| 149 |
|
| 150 |
export async function getProjectById(projectId: number) {
|
| 151 |
const db = await getDb();
|
| 152 |
-
if (!db)
|
|
|
|
|
|
|
| 153 |
|
| 154 |
const result = await db.select().from(projects)
|
| 155 |
.where(eq(projects.id, projectId))
|
|
@@ -160,7 +209,18 @@ export async function getProjectById(projectId: number) {
|
|
| 160 |
|
| 161 |
export async function updateProjectStatus(projectId: number, status: string, finalOutput?: string, finalScore?: number) {
|
| 162 |
const db = await getDb();
|
| 163 |
-
if (!db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
await db.update(projects)
|
| 166 |
.set({
|
|
@@ -184,7 +244,22 @@ export async function addIteration(projectId: number, version: number, data: {
|
|
| 184 |
feedback?: any;
|
| 185 |
}) {
|
| 186 |
const db = await getDb();
|
| 187 |
-
if (!db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
const result = await db.insert(iterations).values({
|
| 190 |
projectId,
|
|
@@ -202,7 +277,11 @@ export async function addIteration(projectId: number, version: number, data: {
|
|
| 202 |
|
| 203 |
export async function getProjectIterations(projectId: number) {
|
| 204 |
const db = await getDb();
|
| 205 |
-
if (!db)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
return await db.select().from(iterations)
|
| 208 |
.where(eq(iterations.projectId, projectId))
|
|
@@ -223,7 +302,26 @@ export async function uploadCodeReference(userId: number, data: {
|
|
| 223 |
tags?: string[];
|
| 224 |
}) {
|
| 225 |
const db = await getDb();
|
| 226 |
-
if (!db)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
const result = await db.insert(codeReferences).values({
|
| 229 |
userId,
|
|
@@ -244,12 +342,10 @@ export async function uploadCodeReference(userId: number, data: {
|
|
| 244 |
|
| 245 |
export async function getUserCodeReferences(userId: number, category?: string) {
|
| 246 |
const db = await getDb();
|
| 247 |
-
if (!db)
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
.where(eq(codeReferences.userId, userId))
|
| 252 |
-
.orderBy(desc(codeReferences.createdAt));
|
| 253 |
}
|
| 254 |
|
| 255 |
return await db.select().from(codeReferences)
|
|
@@ -259,7 +355,11 @@ export async function getUserCodeReferences(userId: number, category?: string) {
|
|
| 259 |
|
| 260 |
export async function searchCodeReferences(userId: number, searchTerm: string) {
|
| 261 |
const db = await getDb();
|
| 262 |
-
if (!db)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
return await db.select().from(codeReferences)
|
| 265 |
.where(eq(codeReferences.userId, userId))
|
|
@@ -269,7 +369,7 @@ export async function searchCodeReferences(userId: number, searchTerm: string) {
|
|
| 269 |
// Statistics queries
|
| 270 |
export async function updateDailyStatistics(date: string) {
|
| 271 |
const db = await getDb();
|
| 272 |
-
if (!db) return
|
| 273 |
|
| 274 |
const projectsData = await db.select({
|
| 275 |
total: sql`COUNT(*) as total`,
|
|
|
|
| 1 |
import { eq, desc, asc, sql } from "drizzle-orm";
|
| 2 |
import { drizzle } from "drizzle-orm/mysql2";
|
| 3 |
+
import { InsertUser, User, users, projects, iterations, codeReferences, statistics } from "../drizzle/schema";
|
| 4 |
import { ENV } from './_core/env';
|
| 5 |
|
| 6 |
let _db: ReturnType<typeof drizzle> | null = null;
|
| 7 |
|
| 8 |
+
// In-memory storage for fallback mode
|
| 9 |
+
const memoryStore = {
|
| 10 |
+
users: [] as User[],
|
| 11 |
+
projects: [] as any[],
|
| 12 |
+
iterations: [] as any[],
|
| 13 |
+
codeReferences: [] as any[],
|
| 14 |
+
statistics: [] as any[],
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
// Lazily create the drizzle instance so local tooling can run without a DB.
|
| 18 |
export async function getDb() {
|
| 19 |
if (!_db && process.env.DATABASE_URL) {
|
| 20 |
try {
|
| 21 |
_db = drizzle(process.env.DATABASE_URL);
|
| 22 |
} catch (error) {
|
| 23 |
+
console.warn("[Database] Failed to connect, using memory fallback:", error);
|
| 24 |
_db = null;
|
| 25 |
}
|
| 26 |
}
|
|
|
|
| 34 |
|
| 35 |
const db = await getDb();
|
| 36 |
if (!db) {
|
| 37 |
+
console.warn("[Database] Using memory fallback for upsertUser");
|
| 38 |
+
const existingIdx = memoryStore.users.findIndex(u => u.openId === user.openId);
|
| 39 |
+
const userData: User = {
|
| 40 |
+
id: existingIdx >= 0 ? memoryStore.users[existingIdx].id : memoryStore.users.length + 1,
|
| 41 |
+
openId: user.openId,
|
| 42 |
+
name: user.name ?? null,
|
| 43 |
+
email: user.email ?? null,
|
| 44 |
+
loginMethod: user.loginMethod ?? null,
|
| 45 |
+
role: user.role ?? (user.openId === ENV.ownerOpenId ? 'admin' : 'user'),
|
| 46 |
+
createdAt: existingIdx >= 0 ? memoryStore.users[existingIdx].createdAt : new Date(),
|
| 47 |
+
updatedAt: new Date(),
|
| 48 |
+
lastSignedIn: user.lastSignedIn ?? new Date(),
|
| 49 |
+
};
|
| 50 |
+
|
| 51 |
+
if (existingIdx >= 0) {
|
| 52 |
+
memoryStore.users[existingIdx] = userData;
|
| 53 |
+
} else {
|
| 54 |
+
memoryStore.users.push(userData);
|
| 55 |
+
}
|
| 56 |
return;
|
| 57 |
}
|
| 58 |
|
|
|
|
| 107 |
export async function getUserByOpenId(openId: string) {
|
| 108 |
const db = await getDb();
|
| 109 |
if (!db) {
|
| 110 |
+
return memoryStore.users.find(u => u.openId === openId);
|
|
|
|
| 111 |
}
|
| 112 |
|
| 113 |
const result = await db.select().from(users).where(eq(users.openId, openId)).limit(1);
|
|
|
|
| 114 |
return result.length > 0 ? result[0] : undefined;
|
| 115 |
}
|
| 116 |
|
|
|
|
| 140 |
originalPrompt: string;
|
| 141 |
}) {
|
| 142 |
const db = await getDb();
|
| 143 |
+
if (!db) {
|
| 144 |
+
const newProject = {
|
| 145 |
+
id: memoryStore.projects.length + 1,
|
| 146 |
+
userId,
|
| 147 |
+
name: data.name,
|
| 148 |
+
description: data.description ?? null,
|
| 149 |
+
mode: data.mode,
|
| 150 |
+
contentType: data.contentType,
|
| 151 |
+
originalPrompt: data.originalPrompt,
|
| 152 |
+
status: "pending",
|
| 153 |
+
createdAt: new Date(),
|
| 154 |
+
updatedAt: new Date(),
|
| 155 |
+
finalOutput: null,
|
| 156 |
+
finalScore: 0,
|
| 157 |
+
completedAt: null,
|
| 158 |
+
};
|
| 159 |
+
memoryStore.projects.push(newProject);
|
| 160 |
+
return newProject;
|
| 161 |
+
}
|
| 162 |
|
| 163 |
const result = await db.insert(projects).values({
|
| 164 |
userId,
|
|
|
|
| 170 |
status: "pending",
|
| 171 |
});
|
| 172 |
|
|
|
|
| 173 |
const projectId = (result as any).insertId || 0;
|
| 174 |
if (projectId > 0) {
|
| 175 |
const newProject = await db.select().from(projects).where(eq(projects.id, projectId)).limit(1);
|
|
|
|
| 181 |
|
| 182 |
export async function getUserProjects(userId: number, limit = 50) {
|
| 183 |
const db = await getDb();
|
| 184 |
+
if (!db) {
|
| 185 |
+
return memoryStore.projects
|
| 186 |
+
.filter(p => p.userId === userId)
|
| 187 |
+
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())
|
| 188 |
+
.slice(0, limit);
|
| 189 |
+
}
|
| 190 |
|
| 191 |
return await db.select().from(projects)
|
| 192 |
.where(eq(projects.userId, userId))
|
|
|
|
| 196 |
|
| 197 |
export async function getProjectById(projectId: number) {
|
| 198 |
const db = await getDb();
|
| 199 |
+
if (!db) {
|
| 200 |
+
return memoryStore.projects.find(p => p.id === projectId) || null;
|
| 201 |
+
}
|
| 202 |
|
| 203 |
const result = await db.select().from(projects)
|
| 204 |
.where(eq(projects.id, projectId))
|
|
|
|
| 209 |
|
| 210 |
export async function updateProjectStatus(projectId: number, status: string, finalOutput?: string, finalScore?: number) {
|
| 211 |
const db = await getDb();
|
| 212 |
+
if (!db) {
|
| 213 |
+
const project = memoryStore.projects.find(p => p.id === projectId);
|
| 214 |
+
if (project) {
|
| 215 |
+
project.status = status;
|
| 216 |
+
if (finalOutput !== undefined) project.finalOutput = finalOutput;
|
| 217 |
+
if (finalScore !== undefined) project.finalScore = finalScore;
|
| 218 |
+
project.updatedAt = new Date();
|
| 219 |
+
if (status === "completed") project.completedAt = new Date();
|
| 220 |
+
return true;
|
| 221 |
+
}
|
| 222 |
+
return false;
|
| 223 |
+
}
|
| 224 |
|
| 225 |
await db.update(projects)
|
| 226 |
.set({
|
|
|
|
| 244 |
feedback?: any;
|
| 245 |
}) {
|
| 246 |
const db = await getDb();
|
| 247 |
+
if (!db) {
|
| 248 |
+
const newIteration = {
|
| 249 |
+
id: memoryStore.iterations.length + 1,
|
| 250 |
+
projectId,
|
| 251 |
+
version,
|
| 252 |
+
qwenOutput: data.qwenOutput ?? null,
|
| 253 |
+
deepseekAnalysis: data.deepseekAnalysis ?? null,
|
| 254 |
+
score: data.score,
|
| 255 |
+
passed: data.passed ? 1 : 0,
|
| 256 |
+
scorecard: data.scorecard ? JSON.stringify(data.scorecard) : null,
|
| 257 |
+
feedback: data.feedback ? JSON.stringify(data.feedback) : null,
|
| 258 |
+
createdAt: new Date(),
|
| 259 |
+
};
|
| 260 |
+
memoryStore.iterations.push(newIteration);
|
| 261 |
+
return { insertId: newIteration.id };
|
| 262 |
+
}
|
| 263 |
|
| 264 |
const result = await db.insert(iterations).values({
|
| 265 |
projectId,
|
|
|
|
| 277 |
|
| 278 |
export async function getProjectIterations(projectId: number) {
|
| 279 |
const db = await getDb();
|
| 280 |
+
if (!db) {
|
| 281 |
+
return memoryStore.iterations
|
| 282 |
+
.filter(i => i.projectId === projectId)
|
| 283 |
+
.sort((a, b) => a.version - b.version);
|
| 284 |
+
}
|
| 285 |
|
| 286 |
return await db.select().from(iterations)
|
| 287 |
.where(eq(iterations.projectId, projectId))
|
|
|
|
| 302 |
tags?: string[];
|
| 303 |
}) {
|
| 304 |
const db = await getDb();
|
| 305 |
+
if (!db) {
|
| 306 |
+
const newRef = {
|
| 307 |
+
id: memoryStore.codeReferences.length + 1,
|
| 308 |
+
userId,
|
| 309 |
+
filename: data.filename,
|
| 310 |
+
category: data.category,
|
| 311 |
+
description: data.description ?? null,
|
| 312 |
+
content: data.content ?? null,
|
| 313 |
+
s3Key: data.s3Key ?? null,
|
| 314 |
+
s3Url: data.s3Url ?? null,
|
| 315 |
+
mimeType: data.mimeType ?? null,
|
| 316 |
+
fileSize: data.fileSize ?? null,
|
| 317 |
+
analysis: data.analysis ? JSON.stringify(data.analysis) : null,
|
| 318 |
+
tags: data.tags ? JSON.stringify(data.tags) : null,
|
| 319 |
+
createdAt: new Date(),
|
| 320 |
+
updatedAt: new Date(),
|
| 321 |
+
};
|
| 322 |
+
memoryStore.codeReferences.push(newRef);
|
| 323 |
+
return { insertId: newRef.id };
|
| 324 |
+
}
|
| 325 |
|
| 326 |
const result = await db.insert(codeReferences).values({
|
| 327 |
userId,
|
|
|
|
| 342 |
|
| 343 |
export async function getUserCodeReferences(userId: number, category?: string) {
|
| 344 |
const db = await getDb();
|
| 345 |
+
if (!db) {
|
| 346 |
+
return memoryStore.codeReferences
|
| 347 |
+
.filter(r => r.userId === userId && (!category || r.category === category))
|
| 348 |
+
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
|
|
|
|
|
| 349 |
}
|
| 350 |
|
| 351 |
return await db.select().from(codeReferences)
|
|
|
|
| 355 |
|
| 356 |
export async function searchCodeReferences(userId: number, searchTerm: string) {
|
| 357 |
const db = await getDb();
|
| 358 |
+
if (!db) {
|
| 359 |
+
return memoryStore.codeReferences
|
| 360 |
+
.filter(r => r.userId === userId && (r.filename.includes(searchTerm) || (r.description && r.description.includes(searchTerm))))
|
| 361 |
+
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
| 362 |
+
}
|
| 363 |
|
| 364 |
return await db.select().from(codeReferences)
|
| 365 |
.where(eq(codeReferences.userId, userId))
|
|
|
|
| 369 |
// Statistics queries
|
| 370 |
export async function updateDailyStatistics(date: string) {
|
| 371 |
const db = await getDb();
|
| 372 |
+
if (!db) return true;
|
| 373 |
|
| 374 |
const projectsData = await db.select({
|
| 375 |
total: sql`COUNT(*) as total`,
|