UNPKG

sensai

Version:

Because even AI needs a master

161 lines (159 loc) 5.01 kB
// utils "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { getMarkdownTypescript: function() { return getMarkdownTypescript; }, getPromptTypescript: function() { return getPromptTypescript; } }); const _graymatter = /*#__PURE__*/ _interop_require_default(require("gray-matter")); const _markdownwasm = /*#__PURE__*/ _interop_require_wildcard(require("markdown-wasm")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } // const defaultSchema = { // type: "object", // oneOf: [ // { // required: ["prompt"], // properties: { // prompt: { // type: "string", // description: "Direct prompt text to the LLM.", // }, // }, // additionalProperties: false, // }, // { // required: ["messages"], // properties: { // messages: { // type: "array", // description: // "Array of message objects for conversational interaction.", // items: { // type: "object", // required: ["role", "content"], // properties: { // role: { // type: "string", // enum: ["system", "user", "assistant", "tool"], // description: "The role defining the author of the message.", // }, // content: { // type: "string", // description: "The message content.", // }, // }, // additionalProperties: false, // }, // minItems: 1, // }, // }, // additionalProperties: false, // }, // ], // }; const defaultSchema = { type: "object", properties: { prompt: { type: "string", description: "The entire prompt provided by the user." } }, required: [ "prompt" ] }; const getPromptTypescript = (fileContent, tools = [] // TODO type Tool should be global )=>{ const { content, data } = (0, _graymatter.default)(fileContent); // TODO we should provide a way for a prompt to inject the result of an // orhter prompt declaratively // TODO we should find a better way for passthrough prompts return ` import template from 'sensai/template'; import ai, { tool } from 'sensai/dist/src/utils/ai'; // TODO this should be cleaner const tools = {}; ${tools.map((tool)=>` tools["${tool.name}"] = tool(require('${tool.path}').default); `).join("\n")} const content = ${JSON.stringify(content.trim())}; const prompt = template(content); const input = content !== '#{prompt}' ? prompt.schema : ${JSON.stringify(defaultSchema)}; export default guard(async (data) => { const text = await prompt(data); return await ai(text, { tools }); }, { description: ${JSON.stringify(data.description)}, input, }); `; }; const getMarkdownTypescript = (fileContent)=>{ const { content, data } = (0, _graymatter.default)(fileContent); // TODO it would be great to have a fast parser like markdown-wasm working with streams const html = _markdownwasm.parse(content); return ` import template from 'sensai/template' const markdown = template(${JSON.stringify(html)}) export default async (data) => { return markdown(data) } `; };