@genkit-ai/flow
Version:
Genkit AI framework workflow APIs.
1 lines • 2.31 kB
Source Map (JSON)
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["/**\n * Copyright 2024 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport { v4 as uuidv4 } from 'uuid';\nimport z from 'zod';\nimport { Context } from './context.js';\n\n/**\n * Adds flows specific prefix for OpenTelemetry span attributes.\n */\nexport function metadataPrefix(name: string) {\n return `flow:${name}`;\n}\n\nconst ctxAsyncLocalStorage = new AsyncLocalStorage<\n Context<z.ZodTypeAny, z.ZodTypeAny, z.ZodTypeAny>\n>();\n\n/**\n * Returns current active context.\n */\nexport function getActiveContext() {\n return ctxAsyncLocalStorage.getStore();\n}\n\n/**\n * Execute the provided function in the flow context. Call {@link getActiveContext()} anywhere\n * within the async call stack to retrieve the context.\n */\nexport function runWithActiveContext<R>(\n ctx: Context<z.ZodTypeAny, z.ZodTypeAny, z.ZodTypeAny>,\n fn: () => R\n) {\n return ctxAsyncLocalStorage.run(ctx, fn);\n}\n\n/**\n * Generates a flow ID.\n */\nexport function generateFlowId() {\n return uuidv4();\n}\n\n/**\n * Gets the auth object from the current context.\n */\nexport function getFlowAuth(): any {\n const ctx = getActiveContext();\n if (!ctx) {\n throw new Error('Can only be run from a flow');\n }\n return ctx.auth;\n}\n"],"mappings":";AAgBA,SAAS,yBAAyB;AAClC,SAAS,MAAM,cAAc;AAOtB,SAAS,eAAe,MAAc;AAC3C,SAAO,QAAQ,IAAI;AACrB;AAEA,MAAM,uBAAuB,IAAI,kBAE/B;AAKK,SAAS,mBAAmB;AACjC,SAAO,qBAAqB,SAAS;AACvC;AAMO,SAAS,qBACd,KACA,IACA;AACA,SAAO,qBAAqB,IAAI,KAAK,EAAE;AACzC;AAKO,SAAS,iBAAiB;AAC/B,SAAO,OAAO;AAChB;AAKO,SAAS,cAAmB;AACjC,QAAM,MAAM,iBAAiB;AAC7B,MAAI,CAAC,KAAK;AACR,UAAM,IAAI,MAAM,6BAA6B;AAAA,EAC/C;AACA,SAAO,IAAI;AACb;","names":[]}