({
projectId,
usage,
requestedAt,
receivedAt,
cacheHit,
reqPayload,
respPayload,
statusCode,
errorMessage,
tags = {},
}: {
projectId: string;
usage?: CalculatedUsage;
requestedAt: number;
receivedAt?: number;
cacheHit: boolean;
reqPayload: unknown;
respPayload?: unknown;
statusCode?: number;
errorMessage?: string;
tags?: Record<string, string>;
})
| 173 | }); |
| 174 | |
| 175 | export const recordLoggedCall = async ({ |
| 176 | projectId, |
| 177 | usage, |
| 178 | requestedAt, |
| 179 | receivedAt, |
| 180 | cacheHit, |
| 181 | reqPayload, |
| 182 | respPayload, |
| 183 | statusCode, |
| 184 | errorMessage, |
| 185 | tags = {}, |
| 186 | }: { |
| 187 | projectId: string; |
| 188 | usage?: CalculatedUsage; |
| 189 | requestedAt: number; |
| 190 | receivedAt?: number; |
| 191 | cacheHit: boolean; |
| 192 | reqPayload: unknown; |
| 193 | respPayload?: unknown; |
| 194 | statusCode?: number; |
| 195 | errorMessage?: string; |
| 196 | tags?: Record<string, string>; |
| 197 | }) => { |
| 198 | const newLoggedCallId = uuidv4(); |
| 199 | |
| 200 | const validatedReqPayload = await reqValidator.spa(reqPayload); |
| 201 | const validatedRespPayload = await chatCompletionOutput.spa(respPayload); |
| 202 | |
| 203 | try { |
| 204 | await prisma.loggedCall.create({ |
| 205 | data: { |
| 206 | id: newLoggedCallId, |
| 207 | projectId, |
| 208 | requestedAt: new Date(requestedAt), |
| 209 | receivedAt: receivedAt ? new Date(receivedAt) : undefined, |
| 210 | cacheHit: cacheHit ?? false, |
| 211 | model: validatedReqPayload.success ? validatedReqPayload.data.model : null, |
| 212 | reqPayload: (reqPayload === null ? Prisma.JsonNull : reqPayload) as Prisma.InputJsonValue, |
| 213 | respPayload: (respPayload === null |
| 214 | ? Prisma.JsonNull |
| 215 | : respPayload) as Prisma.InputJsonValue, |
| 216 | statusCode: statusCode, |
| 217 | errorMessage: errorMessage, |
| 218 | durationMs: receivedAt && receivedAt - requestedAt, |
| 219 | inputTokens: usage?.inputTokens, |
| 220 | outputTokens: usage?.outputTokens, |
| 221 | cost: usage?.cost, |
| 222 | completionId: validatedRespPayload.success ? validatedRespPayload.data.id : null, |
| 223 | }, |
| 224 | }); |
| 225 | } catch (e) { |
| 226 | throw new Error(`Failed to create logged call: ${(e as Error).message}`); |
| 227 | } |
| 228 | |
| 229 | if (Object.keys(tags).length > 0) { |
| 230 | await createTags(projectId, newLoggedCallId, tags); |
| 231 | } |
| 232 | }; |
no test coverage detected