UNPKG

convex

Version:

Client for the Convex Cloud

79 lines (77 loc) 2.6 kB
"use strict"; import { z } from "zod"; import { loadSelectedDeploymentCredentials } from "../../api.js"; import { parseArgs, parseFunctionName } from "../../run.js"; import { readProjectConfig } from "../../config.js"; import { ConvexHttpClient } from "../../../../browser/index.js"; import { Logger } from "../../../../browser/logging.js"; import { getDeploymentSelection } from "../../deploymentSelection.js"; const inputSchema = z.object({ deploymentSelector: z.string().describe( "Deployment selector (from the status tool) to run the function on." ), functionName: z.string().describe( "The name of the function to run (e.g. 'path/to/my/module.js:myFunction')." ), args: z.string().describe( "The argument object to pass to the function, JSON-encoded as a string." ) }); const outputSchema = z.object({ result: z.any().describe("The result returned by the function"), logLines: z.array(z.string()).describe("The log lines generated by the function") }); const description = ` Run a Convex function (query, mutation, or action) on your deployment. Returns the result and any log lines generated by the function. `.trim(); export const RunTool = { name: "run", description, inputSchema, outputSchema, handler: async (ctx, args) => { const { projectDir, deployment } = await ctx.decodeDeploymentSelector( args.deploymentSelector ); process.chdir(projectDir); const metadata = await getDeploymentSelection(ctx, ctx.options); const credentials = await loadSelectedDeploymentCredentials( ctx, metadata, deployment ); const parsedArgs = await parseArgs(ctx, args.args); const { projectConfig } = await readProjectConfig(ctx); const parsedFunctionName = await parseFunctionName( ctx, args.functionName, projectConfig.functions ); const logger = new Logger({ verbose: true }); const logLines = []; logger.addLogLineListener((level, ...args2) => { logLines.push(`${level}: ${args2.join(" ")}`); }); const client = new ConvexHttpClient(credentials.url, { logger }); client.setAdminAuth(credentials.adminKey); let result; try { result = await client.function(parsedFunctionName, void 0, parsedArgs); } catch (err) { return await ctx.crash({ exitCode: 1, errorType: "invalid filesystem or env vars", printedMessage: `Failed to run function "${args.functionName}": ${err.toString().trim()}` }); } return { result, logLines }; } }; //# sourceMappingURL=run.js.map