pomljs
Version:
Prompt Orchestration Markup Language
1 lines • 12 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":["../.build/index.js"],"sourcesContent":["import * as React from 'react';\nimport { readFileSync, writeFileSync } from 'fs';\nimport path from 'path';\nimport { EnvironmentDispatcher } from \"./writer\";\nimport { ErrorCollection, StyleSheetProvider, SystemError, richContentFromSourceMap } from './base';\nimport { PomlFile } from './file';\nimport './presentation';\nimport './essentials';\nimport \"./components\";\nimport { reactRender } from './util/reactRender';\nimport { dumpTrace, setTrace, clearTrace, isTracing, parseJsonWithBuffers } from './util/trace';\nexport { richContentFromSourceMap };\nexport const read = async (element, options, context, stylesheet, sourcePath) => {\n let readElement;\n if (typeof element === 'string') {\n readElement = new PomlFile(element, options, sourcePath).react(context);\n }\n else {\n if (options || context) {\n console.warn('Options and context are ignored when element is React.ReactElement');\n }\n readElement = element;\n }\n if (stylesheet) {\n readElement = React.createElement(StyleSheetProvider, { stylesheet }, readElement);\n }\n return await reactRender(readElement);\n};\n/**\n * Entry point for turning a parsed IR string into rich content or a list of\n * speaker messages. The heavy lifting is done by `EnvironmentDispatcher`.\n */\nexport function write(ir, options) {\n const writer = new EnvironmentDispatcher();\n if (options?.speaker) {\n return writer.writeMessages(ir);\n }\n else {\n return writer.write(ir);\n }\n}\n;\n/**\n * Variant of {@link write} that also exposes a source map describing the\n * mapping between input indices and output content.\n */\nexport function writeWithSourceMap(ir, options) {\n const writer = new EnvironmentDispatcher();\n if (options?.speaker) {\n return writer.writeMessagesWithSourceMap(ir);\n }\n else {\n return writer.writeWithSourceMap(ir);\n }\n}\n;\nexport const poml = async (element) => {\n ErrorCollection.clear();\n const readResult = await read(element);\n const result = write(readResult);\n if (!ErrorCollection.empty()) {\n throw ErrorCollection.first();\n }\n return result;\n};\nexport async function commandLine(args) {\n const readOptions = {\n trim: args.trim,\n };\n if (args.traceDir) {\n setTrace(true, args.traceDir);\n }\n // Determine the working directory\n let workingDirectory;\n if (args.cwd) {\n workingDirectory = path.resolve(args.cwd);\n }\n else {\n workingDirectory = process.cwd();\n }\n let input;\n let sourcePath;\n if (args.input && args.file) {\n throw new Error('Cannot specify both input and file');\n }\n else if (args.input) {\n input = args.input;\n }\n else if (args.file) {\n const filePath = path.resolve(workingDirectory, args.file);\n input = readFileSync(filePath, { encoding: 'utf8' });\n sourcePath = filePath;\n }\n else {\n throw new Error('Must specify either input or file');\n }\n let context = {};\n if (args.context) {\n for (const pair of args.context) {\n if (!pair.includes('=')) {\n throw new Error(`Invalid context variable, must include one '=': ${pair}`);\n }\n const [key, value] = pair.split('=', 2);\n context[key] = value;\n }\n }\n else if (args.contextFile) {\n const contextFilePath = path.resolve(workingDirectory, args.contextFile);\n const contextFromFile = parseJsonWithBuffers(readFileSync(contextFilePath, { encoding: 'utf8' }));\n context = { ...context, ...contextFromFile };\n }\n let stylesheet = {};\n if (args.stylesheetFile) {\n const stylesheetFilePath = path.resolve(workingDirectory, args.stylesheetFile);\n stylesheet = { ...stylesheet, ...parseJsonWithBuffers(readFileSync(stylesheetFilePath, { encoding: 'utf8' })) };\n }\n if (args.stylesheet) {\n stylesheet = { ...stylesheet, ...JSON.parse(args.stylesheet) };\n }\n ErrorCollection.clear();\n const ir = await read(input, readOptions, context, stylesheet, sourcePath);\n const speakerMode = args.speakerMode === true || args.speakerMode === undefined;\n const prettyPrint = args.prettyPrint === true;\n let output = '';\n let result;\n if (prettyPrint) {\n if (speakerMode) {\n result = write(ir, { speaker: true });\n const outputs = result.map((message) => {\n return `===== ${message.speaker} =====\\n\\n${renderContent(message.content)}`;\n });\n output = outputs.join('\\n\\n');\n }\n else {\n result = write(ir);\n output = renderContent(result);\n }\n }\n else {\n result = write(ir, { speaker: speakerMode });\n output = JSON.stringify(result);\n }\n if (isTracing()) {\n try {\n dumpTrace(input, context, stylesheet, result, sourcePath);\n }\n catch (err) {\n ErrorCollection.add(new SystemError('Failed to dump trace', { cause: err }));\n }\n }\n if (args.strict === true || args.strict === undefined) {\n if (!ErrorCollection.empty()) {\n throw ErrorCollection.first();\n }\n }\n if (args.output) {\n const outputPath = path.resolve(workingDirectory, args.output);\n writeFileSync(outputPath, output);\n }\n else {\n process.stdout.write(output);\n }\n}\nconst renderContent = (content) => {\n if (typeof content === 'string') {\n return content;\n }\n const outputs = content.map((part) => {\n if (typeof part === 'string') {\n return part;\n }\n else {\n const media = JSON.stringify(part);\n if (media.length > 100) {\n return media.slice(0, 100) + '...';\n }\n else {\n return media;\n }\n }\n });\n return outputs.join('\\n\\n');\n};\nexport { setTrace, clearTrace, parseJsonWithBuffers, dumpTrace };\n//# sourceMappingURL=index.js.map"],"names":["PomlFile","React","StyleSheetProvider","reactRender","writer","EnvironmentDispatcher","ErrorCollection","setTrace","readFileSync","parseJsonWithBuffers","isTracing","dumpTrace","SystemError","writeFileSync"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYY,MAAC,IAAI,GAAG,OAAO,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,UAAU,KAAK;AACjF,IAAI,IAAI,WAAW;AACnB,IAAI,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;AACrC,QAAQ,WAAW,GAAG,IAAIA,aAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC;AAC/E;AACA,SAAS;AACT,QAAQ,IAAI,OAAO,IAAI,OAAO,EAAE;AAChC,YAAY,OAAO,CAAC,IAAI,CAAC,oEAAoE,CAAC;AAC9F;AACA,QAAQ,WAAW,GAAG,OAAO;AAC7B;AACA,IAAI,IAAI,UAAU,EAAE;AACpB,QAAQ,WAAW,GAAGC,gBAAK,CAAC,aAAa,CAACC,uBAAkB,EAAE,EAAE,UAAU,EAAE,EAAE,WAAW,CAAC;AAC1F;AACA,IAAI,OAAO,MAAMC,uBAAW,CAAC,WAAW,CAAC;AACzC;AACA;AACA;AACA;AACA;AACO,SAAS,KAAK,CAAC,EAAE,EAAE,OAAO,E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