genkit-cli
Version:
CLI for interacting with the Google Genkit AI framework
92 lines • 4.03 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.flowRun = 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.flowRun = new commander_1.Command('flow:run')
.description('run a flow using provided data as input')
.argument('<flowName>', 'name of the flow to run')
.argument('[data]', 'JSON data to use to start the flow')
.option('-w, --wait', 'Wait for the flow to complete', false)
.option('-s, --stream', 'Stream output', false)
.option('-c, --context <JSON>', 'JSON object passed to context', '')
.option('--output <filename>', 'name of the output file to store the extracted data')
.action(async (flowName, data, options) => {
const dashDashIndex = process.argv.indexOf('--');
let runtimeCommand;
let actualData = data;
if (dashDashIndex !== -1) {
const numArgsAfterDashDash = process.argv.length - dashDashIndex - 1;
runtimeCommand = exports.flowRun.args.slice(-numArgsAfterDashDash);
const commandArgs = exports.flowRun.args.slice(0, exports.flowRun.args.length - numArgsAfterDashDash);
if (commandArgs.length > 1) {
actualData = commandArgs[1];
}
else {
actualData = undefined;
}
}
const projectRoot = await (0, utils_1.findProjectRoot)();
const runAction = async (manager) => {
let traceId;
const response = await manager.runAction({
key: `/flow/${flowName}`,
input: actualData ? JSON.parse(actualData) : undefined,
context: options.context ? JSON.parse(options.context) : undefined,
}, options.stream
? (chunk) => console.log(JSON.stringify(chunk, undefined, ' '))
: undefined, (tid) => {
traceId = tid;
});
const result = response.result;
utils_1.logger.info(clc.green('Result:'));
const resultOutput = typeof result === 'string'
? result
: JSON.stringify(result, undefined, ' ');
utils_1.logger.info(resultOutput);
if (traceId) {
utils_1.logger.info(`${clc.cyan('Trace ID:')} ${traceId}`);
}
if (options.output && result) {
await (0, promises_1.writeFile)(options.output, JSON.stringify(result, undefined, ' '));
}
};
await (0, manager_utils_1.runWithManager)(projectRoot, runAction, { runtimeCommand });
});
//# sourceMappingURL=flow-run.js.map