@genkit-ai/core
Version:
Genkit AI framework core libraries.
83 lines • 1.95 kB
JavaScript
import {
action
} from "./action.mjs";
import { SPAN_TYPE_ATTR, runInNewSpan } from "./tracing.mjs";
function flow(config, fn) {
const resolvedConfig = typeof config === "string" ? { name: config } : config;
return flowAction(resolvedConfig, fn);
}
function defineFlow(registry, config, fn) {
const f = flow(config, fn);
registry.registerAction("flow", f);
return f;
}
function flowAction(config, fn) {
return action(
{
actionType: "flow",
name: config.name,
inputSchema: config.inputSchema,
outputSchema: config.outputSchema,
streamSchema: config.streamSchema,
initSchema: config.initSchema,
initJsonSchema: config.initJsonSchema,
metadata: config.metadata
},
async (input, {
sendChunk,
context,
trace,
abortSignal,
streamingRequested,
inputStream,
init
}) => {
const ctx = sendChunk;
ctx.sendChunk = sendChunk;
ctx.context = context;
ctx.trace = trace;
ctx.abortSignal = abortSignal;
ctx.streamingRequested = streamingRequested;
ctx.inputStream = inputStream;
ctx.init = init;
return fn(input, ctx);
}
);
}
function run(name, funcOrInput, fnOrRegistry, _) {
let func;
let input;
let hasInput = false;
if (typeof funcOrInput === "function") {
func = funcOrInput;
} else {
input = funcOrInput;
hasInput = true;
}
if (typeof fnOrRegistry === "function") {
func = fnOrRegistry;
}
if (!func) {
throw new Error("unable to resolve run function");
}
return runInNewSpan(
{
metadata: { name },
labels: {
[SPAN_TYPE_ATTR]: "flowStep"
}
},
async (meta) => {
meta.input = input;
const output = hasInput ? await func(input) : await func();
meta.output = JSON.stringify(output);
return output;
}
);
}
export {
defineFlow,
flow,
run
};
//# sourceMappingURL=flow.mjs.map