UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 5.5 kB
{"version":3,"file":"store-failed-generation.mjs","names":[],"sources":["../../../../src/services/ai/storage/store-failed-generation.ts"],"sourcesContent":["import type { AiGenerateUsage } from \"@lucidcms/types\";\nimport type { CmsAiGenerateRequestFeature } from \"../../../libs/lucid-remote/services/generate-cms-ai.js\";\nimport { AiGenerationsRepository } from \"../../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../../utils/services/types.js\";\nimport { parseStoredTimestamp } from \"../helpers/date-helpers.js\";\nimport getRequestDurationMs from \"../helpers/get-request-duration-ms.js\";\n\nconst storeFailedGeneration: ServiceFn<\n\t[\n\t\t{\n\t\t\trequestId: string;\n\t\t\tfeature?: CmsAiGenerateRequestFeature;\n\t\t\tuserId?: number;\n\t\t\ttargetType?: string;\n\t\t\ttarget?: Record<string, unknown>;\n\t\t\trequestStartedAt?: number;\n\t\t\terrorMessage?: string | null;\n\t\t\tusage?: AiGenerateUsage | null;\n\t\t},\n\t],\n\tundefined\n> = async (context, props) => {\n\tconst AiGenerations = new AiGenerationsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst existingRes = await AiGenerations.selectSingleByRequestId({\n\t\trequestId: props.requestId,\n\t\tselect: [\"id\", \"created_at\", \"status\"],\n\t\ttenantKey: context.request.tenantKey,\n\t});\n\tif (existingRes.error) return existingRes;\n\n\tif (existingRes.data) {\n\t\tif (existingRes.data.status !== \"pending\") {\n\t\t\treturn {\n\t\t\t\terror: undefined,\n\t\t\t\tdata: undefined,\n\t\t\t};\n\t\t}\n\n\t\tconst createdAt = parseStoredTimestamp(existingRes.data.created_at);\n\t\tconst durationMs = Number.isNaN(createdAt.getTime())\n\t\t\t? null\n\t\t\t: getRequestDurationMs(createdAt.getTime());\n\n\t\tconst updateRes = await AiGenerations.updateSingle({\n\t\t\tdata: {\n\t\t\t\t...(props.usage\n\t\t\t\t\t? {\n\t\t\t\t\t\t\tprovider_request_id: props.usage.providerRequestId ?? null,\n\t\t\t\t\t\t\tusage: props.usage,\n\t\t\t\t\t\t\tmodel: props.usage.model,\n\t\t\t\t\t\t\tcost_currency: props.usage.cost.currency,\n\t\t\t\t\t\t\tcost_total_minor: props.usage.cost.totalCostMinor,\n\t\t\t\t\t\t}\n\t\t\t\t\t: {}),\n\t\t\t\tduration_ms: durationMs,\n\t\t\t\tstatus: \"failed\",\n\t\t\t\terror_message: props.errorMessage ?? null,\n\t\t\t},\n\t\t\twhere: [\n\t\t\t\t{\n\t\t\t\t\tkey: \"request_id\",\n\t\t\t\t\toperator: \"=\",\n\t\t\t\t\tvalue: props.requestId,\n\t\t\t\t},\n\t\t\t],\n\t\t\treturning: [\"id\"],\n\t\t});\n\t\tif (updateRes.error) return updateRes;\n\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tif (!props.feature || !props.targetType || !props.target) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst createRes = await AiGenerations.createSingle({\n\t\tdata: {\n\t\t\trequest_id: props.requestId,\n\t\t\tprovider_request_id: null,\n\t\t\tfeature_key: props.feature.key,\n\t\t\tfeature_version: props.feature.version,\n\t\t\ttenant_key: context.request.tenantKey ?? null,\n\t\t\tuser_id: props.userId ?? null,\n\t\t\ttarget_type: props.targetType,\n\t\t\ttarget: props.target,\n\t\t\toutput: null,\n\t\t\tusage: null,\n\t\t\tmodel: null,\n\t\t\tcost_currency: null,\n\t\t\tcost_total_minor: null,\n\t\t\tduration_ms:\n\t\t\t\tprops.requestStartedAt === undefined\n\t\t\t\t\t? null\n\t\t\t\t\t: getRequestDurationMs(props.requestStartedAt),\n\t\t\tstatus: \"failed\",\n\t\t\terror_message: props.errorMessage ?? null,\n\t\t\t...(props.usage\n\t\t\t\t? {\n\t\t\t\t\t\tprovider_request_id: props.usage.providerRequestId ?? null,\n\t\t\t\t\t\tusage: props.usage,\n\t\t\t\t\t\tmodel: props.usage.model,\n\t\t\t\t\t\tcost_currency: props.usage.cost.currency,\n\t\t\t\t\t\tcost_total_minor: props.usage.cost.totalCostMinor,\n\t\t\t\t\t}\n\t\t\t\t: {}),\n\t\t},\n\t\treturning: [\"id\"],\n\t});\n\tif (createRes.error) return createRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default storeFailedGeneration;\n"],"mappings":"sLAOA,MAAM,EAcF,MAAO,EAAS,IAAU,CAC7B,IAAM,EAAgB,IAAI,EACzB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAEM,EAAc,MAAM,EAAc,wBAAwB,CAC/D,UAAW,EAAM,UACjB,OAAQ,CAAC,KAAM,aAAc,QAAQ,EACrC,UAAW,EAAQ,QAAQ,SAC5B,CAAC,EACD,GAAI,EAAY,MAAO,OAAO,EAE9B,GAAI,EAAY,KAAM,CACrB,GAAI,EAAY,KAAK,SAAW,UAC/B,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAY,EAAqB,EAAY,KAAK,UAAU,EAC5D,EAAa,OAAO,MAAM,EAAU,QAAQ,CAAC,EAChD,KACA,EAAqB,EAAU,QAAQ,CAAC,EAErC,EAAY,MAAM,EAAc,aAAa,CAClD,KAAM,CACL,GAAI,EAAM,MACP,CACA,oBAAqB,EAAM,MAAM,mBAAqB,KACtD,MAAO,EAAM,MACb,MAAO,EAAM,MAAM,MACnB,cAAe,EAAM,MAAM,KAAK,SAChC,iBAAkB,EAAM,MAAM,KAAK,cACpC,EACC,CAAC,EACJ,YAAa,EACb,OAAQ,SACR,cAAe,EAAM,cAAgB,IACtC,EACA,MAAO,CACN,CACC,IAAK,aACL,SAAU,IACV,MAAO,EAAM,SACd,CACD,EACA,UAAW,CAAC,IAAI,CACjB,CAAC,EAGD,OAFI,EAAU,MAAc,EAErB,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EAC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