🪢 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
feat(api): add GET /score/[scoreId] (#1242)
Marc Klingena39bbf541d9a
diff --git a/fern/apis/server/definition/score.yml b/fern/apis/server/definition/score.ymlindex 7dc41cf729..65792ac47b 100644--- a/fern/apis/server/definition/score.yml+++ b/fern/apis/server/definition/score.yml@@ -13,7 +13,7 @@ service:request: CreateScoreRequestresponse: commons.Scoreget:- docs: Get scores+ docs: Get a list of scoresmethod: GETpath: /scoresrequest:@@ -24,6 +24,15 @@ service:userId: optional<string>name: optional<string>response: Scores+ get-by-id:+ docs: Get a score+ method: GET+ path: /scores/{scoreId}+ path-parameters:+ scoreId:+ type: string+ docs: The unique langfuse identifier of a score+ response: commons.Scoredelete:docs: Delete a scoremethod: DELETEdiff --git a/generated/postman/collection.json b/generated/postman/collection.jsonindex fe3e2020da..34c1874285 100644--- a/generated/postman/collection.json+++ b/generated/postman/collection.json@@ -641,7 +641,7 @@"_type": "endpoint","name": "Get","request": {- "description": "Get scores",+ "description": "Get a list of scores","url": {"raw": "{{baseUrl}}/api/public/scores?page=&limit=&userId=&name=","host": [@@ -683,6 +683,38 @@},"response": []},+ {+ "_type": "endpoint",+ "name": "Get By Id",+ "request": {+ "description": "Get a score",+ "url": {+ "raw": "{{baseUrl}}/api/public/scores/:scoreId",+ "host": [+ "{{baseUrl}}"+ ],+ "path": [+ "api",+ "public",+ "scores",+ ":scoreId"+ ],+ "query": [],+ "variable": [+ {+ "key": "scoreId",+ "value": "",+ "description": "The unique langfuse identifier of a score"+ }+ ]+ },+ "header": [],+ "method": "GET",+ "auth": null,+ "body": null+ },+ "response": []+ },{"_type": "endpoint","name": "Delete",diff --git a/web/generated/openapi-server/openapi.yml b/web/generated/openapi-server/openapi.ymlindex 93221f66ad..3dee0447bc 100644--- a/web/generated/openapi-server/openapi.yml+++ b/web/generated/openapi-server/openapi.yml@@ -774,7 +774,7 @@ paths:schema:$ref: '#/components/schemas/CreateScoreRequest'get:- description: Get scores+ description: Get a list of scoresoperationId: score_gettags:- Score@@ -837,6 +837,51 @@ paths:schema: {}security: *ref_0/api/public/scores/{scoreId}:+ get:+ description: Get a score+ operationId: score_get-by-id+ tags:+ - Score+ parameters:+ - name: scoreId+ in: path+ description: The unique langfuse identifier of a score+ required: true+ schema:+ type: string+ responses:+ '200':+ description: ''+ content:+ application/json:+ schema:+ $ref: '#/components/schemas/Score'+ '400':+ description: ''+ content:+ application/json:+ schema: {}+ '401':+ description: ''+ content:+ application/json:+ schema: {}+ '403':+ description: ''+ content:+ application/json:+ schema: {}+ '404':+ description: ''+ content:+ application/json:+ schema: {}+ '405':+ description: ''+ content:+ application/json:+ schema: {}+ security: *ref_0delete:description: Delete a scoreoperationId: score_deletediff --git a/web/src/__tests__/scores.servertest.ts b/web/src/__tests__/scores.servertest.tsindex e717e8de60..d85f83ea8a 100644--- a/web/src/__tests__/scores.servertest.ts+++ b/web/src/__tests__/scores.servertest.ts@@ -270,4 +270,47 @@ describe("/api/public/scores API Endpoint", () => {});expect(deletedScore).toBeNull();});++ it("should GET a score", async () => {+ await pruneDatabase();++ const traceId = uuidv4();++ await makeAPICall("POST", "/api/public/traces", {+ id: traceId,+ });+ const generationId = uuidv4();+ await makeAPICall("POST", "/api/public/generations", {+ id: generationId,+ });++ const scoreId = uuidv4();+ await makeAPICall("POST", "/api/public/scores", {+ id: scoreId,+ observationId: generationId,+ name: "score-name",+ value: 100.5,+ traceId: traceId,+ comment: "comment",+ });++ const getScore = await makeAPICall<{+ id: string;+ name: string;+ value: number;+ comment: string;+ traceId: string;+ observationId: string;+ }>("GET", `/api/public/scores/${scoreId}`);++ expect(getScore.status).toBe(200);+ expect(getScore.body).toMatchObject({+ id: scoreId,+ name: "score-name",+ value: 100.5,+ comment: "comment",+ traceId,+ observationId: generationId,+ });+ });});diff --git a/web/src/pages/api/public/scores/[scoreId].ts b/web/src/pages/api/public/scores/[scoreId].tsindex 503e376b6b..0a8d9cf7bc 100644--- a/web/src/pages/api/public/scores/[scoreId].ts+++ b/web/src/pages/api/public/scores/[scoreId].ts@@ -5,7 +5,7 @@ import { cors, runMiddleware } from "@/src/features/public-api/server/cors";import { verifyAuthHeaderAndReturnScope } from "@/src/features/public-api/server/apiAuth";import { isPrismaException } from "@/src/utils/exceptions";-const ScoreDeleteSchema = z.object({+const ScoreSchema = z.object({scoreId: z.string(),});@@ -33,7 +33,7 @@ export default async function handler(});}- const { scoreId } = ScoreDeleteSchema.parse(req.query); // uses query and not body+ const { scoreId } = ScoreSchema.parse(req.query); // uses query and not bodyconst score = await prisma.score.findUnique({select: {@@ -77,6 +77,59 @@ export default async function handler(error: errorMessage,});}+ } else if (req.method === "GET") {+ try {+ // CHECK AUTH+ const authCheck = await verifyAuthHeaderAndReturnScope(+ req.headers.authorization,+ );+ if (!authCheck.validKey)+ return res.status(401).json({+ message: authCheck.error,+ });+ // END CHECK AUTH++ const { scoreId } = ScoreSchema.parse(req.query);++ // CHECK ACCESS SCOPE+ if (authCheck.scope.accessLevel !== "all") {+ return res.status(401).json({+ message:+ "Access denied - need to use basic auth with secret key to GET scores",+ });+ }+ // END CHECK ACCESS SCOPE++ const score = await prisma.score.findUnique({+ where: {+ id: scoreId,+ trace: {+ projectId: authCheck.scope.projectId,+ },+ },+ });++ if (!score) {+ return res.status(404).json({+ message: "Score not found within authorized project",+ });+ }++ return res.status(200).json(score);+ } catch (error: unknown) {+ console.error(error);+ if (isPrismaException(error)) {+ return res.status(500).json({+ error: "Internal Server Error",+ });+ }+ const errorMessage =+ error instanceof Error ? error.message : "An unknown error occurred";+ res.status(400).json({+ message: "Invalid request data",+ error: errorMessage,+ });+ }} else {return res.status(405).json({ message: "Method not allowed" });}