@genkit-ai/core
Version:
Genkit AI framework core libraries.
1 lines • 1.88 kB
Source Map (JSON)
{"version":3,"sources":["../src/async-context.ts"],"sourcesContent":["/**\n * Copyright 2025 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 { GenkitError } from './error.js';\n\nconst asyncContextKey = '__genkit_AsyncContext';\n\n/**\n * @hidden\n */\nexport function getAsyncContext(): AsyncContext {\n if (!global[asyncContextKey]) {\n throw new GenkitError({\n status: 'FAILED_PRECONDITION',\n message: 'Async context is not initialized.',\n });\n }\n return global[asyncContextKey];\n}\n\n/**\n * @hidden\n */\nexport function setAsyncContext(context: AsyncContext) {\n if (global[asyncContextKey]) return;\n global[asyncContextKey] = context;\n}\n\n/**\n * Manages AsyncLocalStorage instances in a single place.\n */\nexport interface AsyncContext {\n /** Retrieves the store/value from async context. */\n getStore<T>(key: string): T | undefined;\n\n /** Exeuctes the callback using the provided store/value for as the async context. */\n run<T, R>(key: string, store: T, callback: () => R): R;\n}\n"],"mappings":"AAgBA,SAAS,mBAAmB;AAE5B,MAAM,kBAAkB;AAKjB,SAAS,kBAAgC;AAC9C,MAAI,CAAC,OAAO,eAAe,GAAG;AAC5B,UAAM,IAAI,YAAY;AAAA,MACpB,QAAQ;AAAA,MACR,SAAS;AAAA,IACX,CAAC;AAAA,EACH;AACA,SAAO,OAAO,eAAe;AAC/B;AAKO,SAAS,gBAAgB,SAAuB;AACrD,MAAI,OAAO,eAAe,EAAG;AAC7B,SAAO,eAAe,IAAI;AAC5B;","names":[]}