UNPKG

@genkit-ai/flow

Version:

Genkit AI framework workflow APIs.

1 lines 2.64 kB
{"version":3,"sources":["../src/steps.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 { Action } from '@genkit-ai/core';\nimport * as z from 'zod';\nimport { getActiveContext } from './utils.js';\n\n/**\n * A flow steap that executes an action with provided input and memoizes the output.\n */\nexport function runAction<I extends z.ZodTypeAny, O extends z.ZodTypeAny>(\n action: Action<I, O>,\n input: z.infer<I>\n): Promise<z.infer<O>> {\n return run(action.__action.name, input, () => action(input));\n}\n\nexport function run<T>(name: string, func: () => Promise<T>): Promise<T>;\nexport function run<T>(\n name: string,\n input: any,\n func: () => Promise<T>\n): Promise<T>;\n\n/**\n * A flow steap that executes the provided function and memoizes the output.\n */\nexport function run<T>(\n name: string,\n funcOrInput: () => Promise<T>,\n fn?: () => Promise<T>\n): Promise<T> {\n const func = arguments.length === 3 ? fn : funcOrInput;\n const input = arguments.length === 3 ? funcOrInput : undefined;\n if (!func) {\n throw new Error('unable to resolve run function');\n }\n const ctx = getActiveContext();\n if (!ctx) throw new Error('can only be run from a flow');\n return ctx.run({ name }, input, func);\n}\n\n/**\n * A helper that takes an array of inputs and maps each input to a run step.\n */\nexport function runMap<I, O>(\n stepName: string,\n input: I[],\n fn: (i: I) => Promise<O>\n): Promise<O[]> {\n return Promise.all(input.map((f) => run(stepName, () => fn(f))));\n}\n"],"mappings":";AAkBA,SAAS,wBAAwB;AAK1B,SAAS,UACd,QACA,OACqB;AACrB,SAAO,IAAI,OAAO,SAAS,MAAM,OAAO,MAAM,OAAO,KAAK,CAAC;AAC7D;AAYO,SAAS,IACd,MACA,aACA,IACY;AACZ,QAAM,OAAO,UAAU,WAAW,IAAI,KAAK;AAC3C,QAAM,QAAQ,UAAU,WAAW,IAAI,cAAc;AACrD,MAAI,CAAC,MAAM;AACT,UAAM,IAAI,MAAM,gCAAgC;AAAA,EAClD;AACA,QAAM,MAAM,iBAAiB;AAC7B,MAAI,CAAC;AAAK,UAAM,IAAI,MAAM,6BAA6B;AACvD,SAAO,IAAI,IAAI,EAAE,KAAK,GAAG,OAAO,IAAI;AACtC;AAKO,SAAS,OACd,UACA,OACA,IACc;AACd,SAAO,QAAQ,IAAI,MAAM,IAAI,CAAC,MAAM,IAAI,UAAU,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;AACjE;","names":[]}