genkit-cli
Version:
CLI for interacting with the Google Genkit AI framework
103 lines • 4.73 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.flowBatchRun = void 0;
const utils_1 = require("@genkit-ai/tools-common/utils");
const clc = __importStar(require("colorette"));
const commander_1 = require("commander");
const promises_1 = require("fs/promises");
const manager_utils_1 = require("../utils/manager-utils");
exports.flowBatchRun = new commander_1.Command('flow:batchRun')
.description('batch run a flow using provided set of data from a file as input')
.argument('<flowName>', 'name of the flow to run')
.argument('<inputFileName>', 'JSON batch data to use to run the flow')
.option('-w, --wait', 'Wait for the flow to complete', false)
.option('-c, --context <JSON>', 'JSON object passed to context', '')
.option('--output <filename>', 'name of the output file to store the output')
.option('--label [label]', 'label flow run in this batch')
.action(async (flowName, fileName, options) => {
const dashDashIndex = process.argv.indexOf('--');
let runtimeCommand;
let actualFileName = fileName;
if (dashDashIndex !== -1) {
const numArgsAfterDashDash = process.argv.length - dashDashIndex - 1;
runtimeCommand = exports.flowBatchRun.args.slice(-numArgsAfterDashDash);
const commandArgs = exports.flowBatchRun.args.slice(0, exports.flowBatchRun.args.length - numArgsAfterDashDash);
actualFileName = commandArgs[1];
}
const projectRoot = await (0, utils_1.findProjectRoot)();
const runAction = async (manager) => {
if (!actualFileName) {
throw new Error('Missing required argument <inputFileName>');
}
const inputData = JSON.parse(await (0, promises_1.readFile)(actualFileName, 'utf8'));
let input = inputData;
if (inputData.length === 0) {
throw new Error('batch input data must be a non-empty array');
}
if (Object.hasOwn(inputData[0], 'input')) {
input = inputData.map((d) => d.input);
}
const outputValues = [];
for (const data of input) {
utils_1.logger.debug(`Running '/flow/${flowName}'...`);
const response = await manager.runAction({
key: `/flow/${flowName}`,
input: data,
context: options.context ? JSON.parse(options.context) : undefined,
telemetryLabels: options.label
? { batchRun: options.label }
: undefined,
});
utils_1.logger.info(clc.green('Result:'));
const resultOutput = typeof response.result === 'string'
? response.result
: JSON.stringify(response.result, undefined, ' ');
utils_1.logger.info(resultOutput);
if (response.telemetry?.traceId) {
utils_1.logger.info(`${clc.cyan('Trace ID:')} ${response.telemetry.traceId}`);
}
outputValues.push({
input: data,
output: response.result,
});
}
if (options.output) {
await (0, promises_1.writeFile)(options.output, JSON.stringify(outputValues, undefined, ' '));
}
};
await (0, manager_utils_1.runWithManager)(projectRoot, runAction, { runtimeCommand });
});
//# sourceMappingURL=flow-batch-run.js.map