UNPKG

@genkit-ai/core

Version:

Genkit AI framework core libraries.

340 lines 12.7 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var __async = (__this, __arguments, generator) => { return new Promise((resolve, reject) => { var fulfilled = (value) => { try { step(generator.next(value)); } catch (e) { reject(e); } }; var rejected = (value) => { try { step(generator.throw(value)); } catch (e) { reject(e); } }; var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); step((generator = generator.apply(__this, __arguments)).next()); }); }; var reflectionApi_exports = {}; __export(reflectionApi_exports, { RunActionResponseSchema: () => RunActionResponseSchema, startReflectionApi: () => startReflectionApi }); module.exports = __toCommonJS(reflectionApi_exports); var import_express = __toESM(require("express")); var import_zod = __toESM(require("zod")); var import_action = require("./action.js"); var import_config = require("./config.js"); var import_logging = require("./logging.js"); var registry = __toESM(require("./registry.js")); var import_schema = require("./schema.js"); var import_tracing = require("./tracing.js"); const RunActionResponseSchema = import_zod.default.object({ result: import_zod.default.unknown().optional(), error: import_zod.default.unknown().optional(), telemetry: import_zod.default.object({ traceId: import_zod.default.string().optional() }).optional() }); let server; const GLOBAL_REFLECTION_API_PORT_KEY = "genkit__reflectionApiPort"; function startReflectionApi(port) { return __async(this, null, function* () { if (global[GLOBAL_REFLECTION_API_PORT_KEY] !== void 0) { import_logging.logger.warn( `Reflection API is already running on port ${global[GLOBAL_REFLECTION_API_PORT_KEY]}` ); return; } if (!port) { port = Number(process.env.GENKIT_REFLECTION_PORT) || 3100; } global[GLOBAL_REFLECTION_API_PORT_KEY] = port; const api = (0, import_express.default)(); api.use(import_express.default.json({ limit: "30mb" })); api.get("/api/__health", (_, response) => __async(this, null, function* () { yield registry.listActions(); response.status(200).send("OK"); })); api.get("/api/__quitquitquit", (_, response) => __async(this, null, function* () { import_logging.logger.debug("Received quitquitquit"); response.status(200).send("OK"); yield stopReflectionApi(); })); api.get("/api/actions", (_, response, next) => __async(this, null, function* () { import_logging.logger.debug("Fetching actions."); const actions = yield registry.listActions(); const convertedActions = {}; Object.keys(actions).forEach((key) => { const action = actions[key].__action; convertedActions[key] = { key, name: action.name, description: action.description, metadata: action.metadata }; if (action.inputSchema || action.inputJsonSchema) { convertedActions[key].inputSchema = (0, import_schema.toJsonSchema)({ schema: action.inputSchema, jsonSchema: action.inputJsonSchema }); } if (action.outputSchema || action.outputJsonSchema) { convertedActions[key].outputSchema = (0, import_schema.toJsonSchema)({ schema: action.outputSchema, jsonSchema: action.outputJsonSchema }); } }); try { response.send(convertedActions); } catch (err) { const { message, stack } = err; next({ message, stack }); } })); api.post("/api/runAction", (request, response, next) => __async(this, null, function* () { const { key, input } = request.body; const { stream } = request.query; import_logging.logger.debug(`Running action \`${key}\`...`); let traceId; try { const action = yield registry.lookupAction(key); if (!action) { response.status(404).send(`action ${key} not found`); return; } if (stream === "true") { const result = yield (0, import_tracing.newTrace)( { name: "dev-run-action-wrapper" }, (_, span) => __async(this, null, function* () { (0, import_tracing.setCustomMetadataAttribute)("genkit-dev-internal", "true"); traceId = span.spanContext().traceId; return yield (0, import_action.runWithStreamingCallback)( (chunk) => { response.write(JSON.stringify(chunk) + "\n"); }, () => __async(this, null, function* () { return yield action(input); }) ); }) ); yield (0, import_tracing.flushTracing)(); response.write( JSON.stringify({ result, telemetry: traceId ? { traceId } : void 0 }) ); response.end(); } else { const result = yield (0, import_tracing.newTrace)( { name: "dev-run-action-wrapper" }, (_, span) => __async(this, null, function* () { (0, import_tracing.setCustomMetadataAttribute)("genkit-dev-internal", "true"); traceId = span.spanContext().traceId; return yield action(input); }) ); response.send({ result, telemetry: traceId ? { traceId } : void 0 }); } } catch (err) { const { message, stack } = err; next({ message, stack, traceId }); } })); api.get("/api/envs", (_, response) => __async(this, null, function* () { response.json(import_config.config.configuredEnvs); })); api.get("/api/envs/:env/traces/:traceId", (request, response) => __async(this, null, function* () { const { env, traceId } = request.params; import_logging.logger.debug(`Fetching trace \`${traceId}\` for env \`${env}\`.`); const tracestore = yield registry.lookupTraceStore(env); if (!tracestore) { return response.status(500).send({ code: import_action.StatusCodes.FAILED_PRECONDITION, message: `${env} trace store not found` }); } try { const trace = yield tracestore == null ? void 0 : tracestore.load(traceId); return trace ? response.json(trace) : response.status(404).send({ code: import_action.StatusCodes.NOT_FOUND, message: `Trace with traceId=${traceId} not found.` }); } catch (err) { const error = err; const { message, stack } = error; const errorResponse = { code: import_action.StatusCodes.INTERNAL, message, details: { stack } }; return response.status(500).json(errorResponse); } })); api.get("/api/envs/:env/traces", (request, response, next) => __async(this, null, function* () { const { env } = request.params; const { limit, continuationToken } = request.query; import_logging.logger.debug(`Fetching traces for env \`${env}\`.`); const tracestore = yield registry.lookupTraceStore(env); if (!tracestore) { return response.status(500).send({ code: import_action.StatusCodes.FAILED_PRECONDITION, message: `${env} trace store not found` }); } try { response.json( yield tracestore.list({ limit: limit ? parseInt(limit.toString()) : void 0, continuationToken: continuationToken ? continuationToken.toString() : void 0 }) ); } catch (err) { const { message, stack } = err; next({ message, stack }); } })); api.get( "/api/envs/:env/flowStates/:flowId", (request, response, next) => __async(this, null, function* () { const { env, flowId } = request.params; import_logging.logger.debug(`Fetching flow state \`${flowId}\` for env \`${env}\`.`); const flowStateStore = yield registry.lookupFlowStateStore(env); if (!flowStateStore) { return response.status(500).send({ code: import_action.StatusCodes.FAILED_PRECONDITION, message: `${env} flow state store not found` }); } try { response.json(yield flowStateStore == null ? void 0 : flowStateStore.load(flowId)); } catch (err) { const { message, stack } = err; next({ message, stack }); } }) ); api.get("/api/envs/:env/flowStates", (request, response, next) => __async(this, null, function* () { const { env } = request.params; const { limit, continuationToken } = request.query; import_logging.logger.debug(`Fetching traces for env \`${env}\`.`); const flowStateStore = yield registry.lookupFlowStateStore(env); if (!flowStateStore) { return response.status(500).send({ code: import_action.StatusCodes.FAILED_PRECONDITION, message: `${env} flow state store not found` }); } try { response.json( yield flowStateStore == null ? void 0 : flowStateStore.list({ limit: limit ? parseInt(limit.toString()) : void 0, continuationToken: continuationToken ? continuationToken.toString() : void 0 }) ); } catch (err) { const { message, stack } = err; next({ message, stack }); } })); api.use((err, req, res, next) => { import_logging.logger.error(err.stack); const error = err; const { message, stack } = error; const errorResponse = { code: import_action.StatusCodes.INTERNAL, message, details: { stack } }; if (err.traceId) { errorResponse.details.traceId = err.traceId; } res.status(500).json(errorResponse); }); server = api.listen(port, () => { console.log(`Reflection API running on http://localhost:${port}`); }); server.on("error", (error) => { if (process.env.GENKIT_REFLECTION_ON_STARTUP_FAILURE === "ignore") { import_logging.logger.warn( `Failed to start the Reflection API on port ${port}, ignoring the error.` ); import_logging.logger.debug(error); } else { throw error; } }); process.on("SIGTERM", () => __async(this, null, function* () { return yield stopReflectionApi(); })); }); } function stopReflectionApi() { return __async(this, null, function* () { yield Promise.all([ new Promise((resolve) => { if (server) { server.close(() => { import_logging.logger.info("Reflection API has succesfully shut down."); resolve(); }); } else { resolve(); } }), (0, import_tracing.cleanUpTracing)() ]); process.exit(0); }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RunActionResponseSchema, startReflectionApi }); //# sourceMappingURL=reflectionApi.js.map