🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
Improve prisma upsert (#80)
Marc Klingen7c2a29aae826
diff --git a/package-lock.json b/package-lock.jsonindex 82e271e6e4..dcafd066b2 100644--- a/package-lock.json+++ b/package-lock.json@@ -1889,6 +1889,126 @@"node": ">= 10"}},+ "node_modules/@next/swc-darwin-x64": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.12.tgz",+ "integrity": "sha512-WRvH7RxgRHlC1yb5oG0ZLx8F7uci9AivM5/HGGv9ZyG2Als8Ij64GC3d+mQ5sJhWjusyU6T6V1WKTUoTmOB0zQ==",+ "cpu": [+ "x64"+ ],+ "optional": true,+ "os": [+ "darwin"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@next/swc-linux-arm64-gnu": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.12.tgz",+ "integrity": "sha512-YEKracAWuxp54tKiAvvq73PUs9lok57cc8meYRibTWe/VdPB2vLgkTVWFcw31YDuRXdEhdX0fWS6Q+ESBhnEig==",+ "cpu": [+ "arm64"+ ],+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@next/swc-linux-arm64-musl": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.12.tgz",+ "integrity": "sha512-LhJR7/RAjdHJ2Isl2pgc/JaoxNk0KtBgkVpiDJPVExVWA1c6gzY57+3zWuxuyWzTG+fhLZo2Y80pLXgIJv7g3g==",+ "cpu": [+ "arm64"+ ],+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@next/swc-linux-x64-gnu": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.12.tgz",+ "integrity": "sha512-1DWLL/B9nBNiQRng+1aqs3OaZcxC16Nf+mOnpcrZZSdyKHek3WQh6j/fkbukObgNGwmCoVevLUa/p3UFTTqgqg==",+ "cpu": [+ "x64"+ ],+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@next/swc-linux-x64-musl": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.12.tgz",+ "integrity": "sha512-kEAJmgYFhp0VL+eRWmUkVxLVunn7oL9Mdue/FS8yzRBVj7Z0AnIrHpTIeIUl1bbdQq1VaoOztnKicAjfkLTRCQ==",+ "cpu": [+ "x64"+ ],+ "optional": true,+ "os": [+ "linux"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@next/swc-win32-arm64-msvc": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.12.tgz",+ "integrity": "sha512-GMLuL/loR6yIIRTnPRY6UGbLL9MBdw2anxkOnANxvLvsml4F0HNIgvnU3Ej4BjbqMTNjD4hcPFdlEow4XHPdZA==",+ "cpu": [+ "arm64"+ ],+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@next/swc-win32-ia32-msvc": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.12.tgz",+ "integrity": "sha512-PhgNqN2Vnkm7XaMdRmmX0ZSwZXQAtamBVSa9A/V1dfKQCV1rjIZeiy/dbBnVYGdj63ANfsOR/30XpxP71W0eww==",+ "cpu": [+ "ia32"+ ],+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ }+ },+ "node_modules/@next/swc-win32-x64-msvc": {+ "version": "13.4.12",+ "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.12.tgz",+ "integrity": "sha512-Z+56e/Ljt0bUs+T+jPjhFyxYBcdY2RIq9ELFU+qAMQMteHo7ymbV7CKmlcX59RI9C4YzN8PgMgLyAoi916b5HA==",+ "cpu": [+ "x64"+ ],+ "optional": true,+ "os": [+ "win32"+ ],+ "engines": {+ "node": ">= 10"+ }+ },"node_modules/@nodelib/fs.scandir": {"version": "2.1.5","resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",@@ -13454,126 +13574,6 @@"funding": {"url": "https://github.com/sponsors/colinhacks"}- },- "node_modules/@next/swc-darwin-x64": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-darwin-x64/-/swc-darwin-x64-13.4.12.tgz",- "integrity": "sha512-WRvH7RxgRHlC1yb5oG0ZLx8F7uci9AivM5/HGGv9ZyG2Als8Ij64GC3d+mQ5sJhWjusyU6T6V1WKTUoTmOB0zQ==",- "cpu": [- "x64"- ],- "optional": true,- "os": [- "darwin"- ],- "engines": {- "node": ">= 10"- }- },- "node_modules/@next/swc-linux-arm64-gnu": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.12.tgz",- "integrity": "sha512-YEKracAWuxp54tKiAvvq73PUs9lok57cc8meYRibTWe/VdPB2vLgkTVWFcw31YDuRXdEhdX0fWS6Q+ESBhnEig==",- "cpu": [- "arm64"- ],- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">= 10"- }- },- "node_modules/@next/swc-linux-arm64-musl": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.12.tgz",- "integrity": "sha512-LhJR7/RAjdHJ2Isl2pgc/JaoxNk0KtBgkVpiDJPVExVWA1c6gzY57+3zWuxuyWzTG+fhLZo2Y80pLXgIJv7g3g==",- "cpu": [- "arm64"- ],- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">= 10"- }- },- "node_modules/@next/swc-linux-x64-gnu": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.12.tgz",- "integrity": "sha512-1DWLL/B9nBNiQRng+1aqs3OaZcxC16Nf+mOnpcrZZSdyKHek3WQh6j/fkbukObgNGwmCoVevLUa/p3UFTTqgqg==",- "cpu": [- "x64"- ],- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">= 10"- }- },- "node_modules/@next/swc-linux-x64-musl": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.12.tgz",- "integrity": "sha512-kEAJmgYFhp0VL+eRWmUkVxLVunn7oL9Mdue/FS8yzRBVj7Z0AnIrHpTIeIUl1bbdQq1VaoOztnKicAjfkLTRCQ==",- "cpu": [- "x64"- ],- "optional": true,- "os": [- "linux"- ],- "engines": {- "node": ">= 10"- }- },- "node_modules/@next/swc-win32-arm64-msvc": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.12.tgz",- "integrity": "sha512-GMLuL/loR6yIIRTnPRY6UGbLL9MBdw2anxkOnANxvLvsml4F0HNIgvnU3Ej4BjbqMTNjD4hcPFdlEow4XHPdZA==",- "cpu": [- "arm64"- ],- "optional": true,- "os": [- "win32"- ],- "engines": {- "node": ">= 10"- }- },- "node_modules/@next/swc-win32-ia32-msvc": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.12.tgz",- "integrity": "sha512-PhgNqN2Vnkm7XaMdRmmX0ZSwZXQAtamBVSa9A/V1dfKQCV1rjIZeiy/dbBnVYGdj63ANfsOR/30XpxP71W0eww==",- "cpu": [- "ia32"- ],- "optional": true,- "os": [- "win32"- ],- "engines": {- "node": ">= 10"- }- },- "node_modules/@next/swc-win32-x64-msvc": {- "version": "13.4.12",- "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.12.tgz",- "integrity": "sha512-Z+56e/Ljt0bUs+T+jPjhFyxYBcdY2RIq9ELFU+qAMQMteHo7ymbV7CKmlcX59RI9C4YzN8PgMgLyAoi916b5HA==",- "cpu": [- "x64"- ],- "optional": true,- "os": [- "win32"- ],- "engines": {- "node": ">= 10"- }}}}diff --git a/src/pages/api/public/generations.ts b/src/pages/api/public/generations.tsindex 6f78f5c057..cd4c2b61d3 100644--- a/src/pages/api/public/generations.ts+++ b/src/pages/api/public/generations.ts@@ -157,10 +157,24 @@ export default async function handler(const newId = uuidv4();+ // Check before upsert as Prisma only upserts in DB transaction when using unique key in select+ // Including projectid would lead to race conditions and unique key errors+ const observationWithSameId = await prisma.observation.count({+ where: {+ id: id ?? newId,+ projectId: {+ not: authCheck.scope.projectId,+ },+ },+ });+ if (observationWithSameId > 0)+ throw new Error(+ "Observation with same id already exists in another project"+ );+const newObservation = await prisma.observation.upsert({where: {id: id ?? newId,- projectId: authCheck.scope.projectId,},create: {id: id ?? newId,@@ -276,10 +290,24 @@ export default async function handler((newPromptTokens ?? existingObservation?.promptTokens ?? 0) +(newCompletionTokens ?? existingObservation?.completionTokens ?? 0);+ // Check before upsert as Prisma only upserts in DB transaction when using unique key in select+ // Including projectid would lead to race conditions and unique key errors+ const observationWithSameId = await prisma.observation.count({+ where: {+ id: generationId,+ projectId: {+ not: authCheck.scope.projectId,+ },+ },+ });+ if (observationWithSameId > 0)+ throw new Error(+ "Observation with same id already exists in another project"+ );+const newObservation = await prisma.observation.upsert({where: {id: generationId,- projectId: authCheck.scope.projectId,},create: {id: generationId,diff --git a/src/pages/api/public/spans.ts b/src/pages/api/public/spans.tsindex 2b5ab6c52d..3a00088f79 100644--- a/src/pages/api/public/spans.ts+++ b/src/pages/api/public/spans.ts@@ -101,10 +101,24 @@ export default async function handler(const newId = uuidv4();+ // Check before upsert as Prisma only upserts in DB transaction when using unique key in select+ // Including projectid would lead to race conditions and unique key errors+ const observationWithSameId = await prisma.observation.count({+ where: {+ id: id ?? newId,+ projectId: {+ not: authCheck.scope.projectId,+ },+ },+ });+ if (observationWithSameId > 0)+ throw new Error(+ "Observation with same id already exists in another project"+ );+const newObservation = await prisma.observation.upsert({where: {id: id ?? newId,- projectId: authCheck.scope.projectId,},create: {id: id ?? newId,@@ -154,10 +168,24 @@ export default async function handler(console.log("Trying to update span: ", req.body);const { spanId, endTime, ...fields } = SpanPatchSchema.parse(req.body);+ // Check before upsert as Prisma only upserts in DB transaction when using unique key in select+ // Including projectid would lead to race conditions and unique key errors+ const observationWithSameId = await prisma.observation.count({+ where: {+ id: spanId,+ projectId: {+ not: authCheck.scope.projectId,+ },+ },+ });+ if (observationWithSameId > 0)+ throw new Error(+ "Observation with same id already exists in another project"+ );+const newObservation = await prisma.observation.upsert({where: {id: spanId,- projectId: authCheck.scope.projectId,},create: {id: spanId,