UNPKG

@prism-lang/core

Version:

A programming language for uncertainty

104 lines 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runPrism = runPrism; exports.createPrismRuntime = createPrismRuntime; const parser_1 = require("./parser"); const runtime_1 = require("./runtime"); /** * Simple helper to run Prism code in one call * * @example * ```javascript * const { runPrism } = require('@prism-lang/core'); * const result = await runPrism('x = 5 ~> 0.9; x * 2'); * console.log(result); // 10 with 90% confidence * ``` * * @example With LLM provider * ```javascript * const { runPrism } = require('@prism-lang/core'); * const { createProvider } = require('@prism-lang/llm'); * * const result = await runPrism( * 'response = llm("Hello"); response', * { llmProvider: createProvider() } * ); * ``` */ async function runPrism(code, options) { // If we have globals, we need to prepend them as assignments let fullCode = code; if (options?.globals) { const globalAssignments = Object.entries(options.globals) .map(([name, value]) => { if (typeof value === 'string') { return `${name} = "${value.replace(/"/g, '\\"')}"`; } else if (typeof value === 'number' || typeof value === 'boolean') { return `${name} = ${value}`; } else if (typeof value === 'function') { // For functions, we can't inject them this way // This is a limitation we need to document return ''; } else { return `${name} = ${JSON.stringify(value)}`; } }) .filter(Boolean) .join('\n'); if (globalAssignments) { fullCode = globalAssignments + '\n' + code; } } // Parse the code const ast = (0, parser_1.parse)(fullCode); // Create runtime const runtime = (0, runtime_1.createRuntime)(); // Set up LLM provider if provided if (options?.llmProvider) { const providerName = options.defaultProviderName || 'default'; runtime.registerLLMProvider(providerName, options.llmProvider); runtime.setDefaultLLMProvider(providerName); } // Execute and return result return runtime.execute(ast); } /** * Create a configured Prism runtime * Useful when you want to reuse the same runtime for multiple executions * * Note: Since the runtime doesn't support direct global injection, * you'll need to execute code that sets globals first. * * @example * ```javascript * const { createPrismRuntime, parse } = require('@prism-lang/core'); * * const runtime = createPrismRuntime(); * * // Set globals by executing assignment code * const setupAst = parse('PI = 3.14159'); * await runtime.execute(setupAst); * * const ast1 = parse('x = PI * 2'); * await runtime.execute(ast1); * * const ast2 = parse('x + 1'); * const result = await runtime.execute(ast2); * ``` */ function createPrismRuntime(options) { const runtime = (0, runtime_1.createRuntime)(); // Set up LLM provider if provided if (options?.llmProvider) { const providerName = options.defaultProviderName || 'default'; runtime.registerLLMProvider(providerName, options.llmProvider); runtime.setDefaultLLMProvider(providerName); } // Note: globals option is ignored here since Runtime doesn't support setGlobal // Users should execute assignment statements instead return runtime; } //# sourceMappingURL=helpers.js.map