import * as Stdlib_Option from '@rescript/runtime/lib/es6/Stdlib_Option.js'; import * as Stdlib_JsError from '@rescript/runtime/lib/es6/Stdlib_JsError.js'; import * as FrontmanCore__Hosts$FrontmanAiFrontmanCore from '@frontman-ai/frontman-core/src/FrontmanCore__Hosts.res.mjs'; import * as Stdlib_JsExn from '@rescript/runtime/lib/es6/Stdlib_JsExn.js'; import * as Stdlib_Promise from '@rescript/runtime/lib/es6/Stdlib_Promise.js'; import * as Primitive_option from '@rescript/runtime/lib/es6/Primitive_option.js'; import { readFileSync } from 'node:fs'; import * as FrontmanCore__Tool__GetLogs$FrontmanAiFrontmanCore from '@frontman-ai/frontman-core/src/tools/FrontmanCore__Tool__GetLogs.res.mjs'; import * as FrontmanCore__Tool__EditFile$FrontmanAiFrontmanCore from '@frontman-ai/frontman-core/src/tools/FrontmanCore__Tool__EditFile.res.mjs'; import * as FrontmanCore__Tool__EditFileWithLogCheck$FrontmanAiFrontmanCore from '@frontman-ai/frontman-core/src/tools/FrontmanCore__Tool__EditFileWithLogCheck.res.mjs'; import * as FrontmanCore__ToolRegistry$FrontmanAiFrontmanCore from '@frontman-ai/frontman-core/src/FrontmanCore__ToolRegistry.res.mjs'; import * as FrontmanCore__Middleware$FrontmanAiFrontmanCore from '@frontman-ai/frontman-core/src/FrontmanCore__Middleware.res.mjs'; import * as FrontmanCore__LogCapture$FrontmanAiFrontmanCore from '@frontman-ai/frontman-core/src/FrontmanCore__LogCapture.res.mjs'; // Generated by ReScript, PLEASE EDIT WITH CARE let packageVersion = (typeof __PACKAGE_VERSION__ !== "undefined" ? __PACKAGE_VERSION__ : undefined); if (typeof packageVersion === "undefined") { Stdlib_JsError.throwWithMessage("__PACKAGE_VERSION__ is not defined — tsup build is misconfigured"); } let host = process.env["FRONTMAN_HOST"]; let defaultHost = host !== undefined ? host : FrontmanCore__Hosts$FrontmanAiFrontmanCore.apiHost; function normalizeHost(host) { let trimmed = host.trim(); let candidate = trimmed.includes("://") ? trimmed : "https://" + trimmed; try { let parsed = new URL(candidate); let port = parsed.port; let tmp; switch (port) { case "" : case "443" : tmp = parsed.hostname; break; default: tmp = parsed.hostname + `:` + port; } return tmp.toLowerCase(); } catch (exn) { return trimmed.toLowerCase(); } } function makeFromObject(config) { let host = normalizeHost(Stdlib_Option.getOr(config.host, defaultHost)); let isDev = Stdlib_Option.getOr(config.isDev, host !== FrontmanCore__Hosts$FrontmanAiFrontmanCore.apiHost.toLowerCase()); let projectRoot = Stdlib_Option.getOr(Stdlib_Option.orElse(config.projectRoot, Stdlib_Option.orElse(process.env["PROJECT_ROOT"], process.env["PWD"])), "."); let sourceRoot = Stdlib_Option.getOr(config.sourceRoot, projectRoot); let basePath = Stdlib_Option.getOr(config.basePath, "frontman"); let serverName = Stdlib_Option.getOr(config.serverName, "frontman-vite"); let serverVersion = Stdlib_Option.getOr(config.serverVersion, packageVersion); let baseUrl = Stdlib_Option.getOr(config.clientUrl, Stdlib_Option.getOr(process.env["FRONTMAN_CLIENT_URL"], isDev ? FrontmanCore__Hosts$FrontmanAiFrontmanCore.devClientJs : FrontmanCore__Hosts$FrontmanAiFrontmanCore.clientJs)); let url = new URL(baseUrl); if (url.searchParams.has("clientName")) ; else { url.searchParams.set("clientName", "vite"); } if (url.searchParams.has("host")) ; else { url.searchParams.set("host", host); } let clientUrl = url.href; return { isDev: isDev, projectRoot: projectRoot, sourceRoot: sourceRoot, basePath: basePath, serverName: serverName, serverVersion: serverVersion, host: host, clientUrl: clientUrl, clientCssUrl: Stdlib_Option.orElse(config.clientCssUrl, isDev ? undefined : FrontmanCore__Hosts$FrontmanAiFrontmanCore.clientCss), entrypointUrl: config.entrypointUrl }; } /* packageVersion Not a pure module */ /** * @returns {import('vite').Plugin} */ function frontmanVueSourcePlugin$1() { return { name: 'frontman:vue-source', enforce: 'post', apply: 'serve', transform(code, id) { if (!id.endsWith('.vue')) return null; if (id.includes('?')) return null; if (!code.includes('__file')) return null; try { const originalSource = readFileSync(id, 'utf-8'); const stripped = originalSource.replace( /<(script|style)\b[\s\S]*?<\/\1>/gi, (m) => '\n'.repeat(m.split('\n').length - 1), ); const templateMatch = stripped.match(/^]/m); if (!templateMatch) return null; const templateLine = stripped.slice(0, templateMatch.index).split('\n').length; const fileAssignPattern = /(\w+)\.__file\s*=\s*"[^"]*"/; const varMatch = code.match(fileAssignPattern); if (!varMatch) { console.warn(`[Frontman] Vue source plugin: no __file assignment found in compiled output for ${id}`); return null; } const varName = varMatch[1]; const transformed = code.replace( fileAssignPattern, `$&\n${varName}.__frontman_templateLine = ${templateLine}` ); return { code: transformed, map: null }; } catch (err) { console.warn(`[Frontman] Vue source plugin: failed to read ${id}:`, err.message); return null; } }, }; } // Generated by ReScript, PLEASE EDIT WITH CARE function frontmanVueSourcePlugin(prim) { return frontmanVueSourcePlugin$1(); } /* ./vite-plugin-vue-source.mjs Not a pure module */ // Generated by ReScript, PLEASE EDIT WITH CARE let name$1 = FrontmanCore__Tool__GetLogs$FrontmanAiFrontmanCore.name; let access$1 = FrontmanCore__Tool__GetLogs$FrontmanAiFrontmanCore.access; let description$1 = FrontmanCore__Tool__GetLogs$FrontmanAiFrontmanCore.description; let inputSchema$1 = FrontmanCore__Tool__GetLogs$FrontmanAiFrontmanCore.inputSchema; let visibleToAgent = true; let outputJsonSchema$1 = FrontmanCore__Tool__GetLogs$FrontmanAiFrontmanCore.outputJsonSchema; let execute$1 = FrontmanCore__Tool__GetLogs$FrontmanAiFrontmanCore.execute; /* FrontmanCore__Tool__GetLogs-FrontmanAiFrontmanCore Not a pure module */ // Generated by ReScript, PLEASE EDIT WITH CARE async function execute(ctx, input) { return await FrontmanCore__Tool__EditFileWithLogCheck$FrontmanAiFrontmanCore.execute(ctx, input, FrontmanCore__Tool__EditFileWithLogCheck$FrontmanAiFrontmanCore.getCoreErrorLogsSince); } let name = "edit_file"; let access = "read-write"; let description = FrontmanCore__Tool__EditFile$FrontmanAiFrontmanCore.description; let inputSchema = FrontmanCore__Tool__EditFile$FrontmanAiFrontmanCore.inputSchema; let outputJsonSchema = FrontmanCore__Tool__EditFile$FrontmanAiFrontmanCore.outputJsonSchema; /* FrontmanCore__Tool__EditFile-FrontmanAiFrontmanCore Not a pure module */ // Generated by ReScript, PLEASE EDIT WITH CARE let viteTools = [{ name: name$1, description: description$1, access: access$1, inputSchema: inputSchema$1, outputJsonSchema: outputJsonSchema$1, execute: execute$1, visibleToAgent: visibleToAgent }]; function make() { return FrontmanCore__ToolRegistry$FrontmanAiFrontmanCore.replaceByName(FrontmanCore__ToolRegistry$FrontmanAiFrontmanCore.addTools(FrontmanCore__ToolRegistry$FrontmanAiFrontmanCore.coreTools(), viteTools), { name: name, description: description, access: access, inputSchema: inputSchema, outputJsonSchema: outputJsonSchema, execute: execute, visibleToAgent: true }); } /* FrontmanVite__Tool__GetLogs-FrontmanAiVite Not a pure module */ // Generated by ReScript, PLEASE EDIT WITH CARE function toMiddlewareConfig(config) { return { projectRoot: config.projectRoot, sourceRoot: config.sourceRoot, basePath: config.basePath, serverName: config.serverName, serverVersion: config.serverVersion, clientUrl: config.clientUrl, clientCssUrl: config.clientCssUrl, entrypointUrl: config.entrypointUrl, frameworkId: "Vite", traits: [] }; } function createMiddleware$1(config) { let registry = make(); let middlewareConfig = toMiddlewareConfig(config); return FrontmanCore__Middleware$FrontmanAiFrontmanCore.createMiddleware(middlewareConfig, registry); } /* FrontmanVite__ToolRegistry-FrontmanAiVite Not a pure module */ // Generated by ReScript, PLEASE EDIT WITH CARE let headersToDict = (function headersToDict(headers) { const dict = {}; headers.forEach(function(value, key) { dict[key] = value; }); return dict; }); let collectBody = (async function collectBody(req) { const chunks = []; for await (const chunk of req) { chunks.push(chunk); } return Buffer.concat(chunks); }); let pipeStreamToResponse = (async function pipeStreamToResponse(stream, res) { const reader = stream.getReader(); try { while (true) { const { done, value } = await reader.read(); if (done) break; res.write(value); } } finally { reader.releaseLock(); } }); function adaptMiddlewareToVite(basePath, middleware) { return async (req, res, next) => { let reqUrl = Stdlib_Option.getOr(Primitive_option.fromNull(req.url), "/"); let pathname = reqUrl.toLowerCase(); let idx = pathname.indexOf("?"); let pathOnly = idx !== -1 ? pathname.slice(0, idx) : pathname; let isFrontmanRoute = FrontmanCore__Middleware$FrontmanAiFrontmanCore.isFrontmanRoute(pathOnly, basePath, Stdlib_Option.getOr(Primitive_option.fromNull(req.method), "GET")); if (!isFrontmanRoute) { return next(); } let bodyBuffer = await collectBody(req); let host = Stdlib_Option.getOr(req.headers["host"], "localhost"); let url = `http://` + host + reqUrl; let method = Stdlib_Option.getOr(Primitive_option.fromNull(req.method), "GET"); let headers = req.headers; let hasBody = bodyBuffer.length > 0; let body = hasBody ? Primitive_option.some(bodyBuffer) : undefined; let webRequest = new Request(url, { method: method, headers: Primitive_option.some(headers), body: body }); let responseOption = await middleware(webRequest); if (responseOption === undefined) { return next(); } res.statusCode = responseOption.status; let headerDict = headersToDict(responseOption.headers); res.writeHead(responseOption.status, headerDict); let stream = responseOption.body; if (stream !== null) { await pipeStreamToResponse(stream, res); } res.end(); }; } function frontmanPlugin$1(options) { let opts = Stdlib_Option.getOr(options, {}); let middlewarePlugin_configureServer = server => { FrontmanCore__LogCapture$FrontmanAiFrontmanCore.initialize(undefined, undefined); let isDev = opts.isDev; let basePath = opts.basePath; let clientUrl = opts.clientUrl; let clientCssUrl = opts.clientCssUrl; let entrypointUrl = opts.entrypointUrl; let projectRoot = opts.projectRoot; let sourceRoot = opts.sourceRoot; let host = opts.host; let configInput = { isDev: isDev, projectRoot: projectRoot, sourceRoot: sourceRoot, basePath: basePath, host: host, clientUrl: clientUrl, clientCssUrl: clientCssUrl, entrypointUrl: entrypointUrl }; let config = makeFromObject(configInput); let middleware = createMiddleware$1(config); let adaptedMiddleware = adaptMiddlewareToVite(config.basePath, middleware); server.middlewares.use((req, res, next) => { Stdlib_Promise.$$catch(adaptedMiddleware(req, res, next), error => { let msg = Stdlib_Option.getOr(Stdlib_Option.flatMap(Stdlib_JsExn.fromException(error), Stdlib_JsExn.message), "Unknown error"); console.error("Frontman middleware error:", msg); res.statusCode = 500; res.end("Internal Server Error"); return Promise.resolve(); }); }); }; let middlewarePlugin = { name: "frontman", configureServer: middlewarePlugin_configureServer }; return [ middlewarePlugin, frontmanVueSourcePlugin() ]; } /* collectBody Not a pure module */ // Generated by ReScript, PLEASE EDIT WITH CARE let Config; let Middleware; let Server; let ToolRegistry; let Plugin; let SSE; let createMiddleware = createMiddleware$1; let makeConfig = makeFromObject; let frontmanPlugin = frontmanPlugin$1; /* FrontmanVite__Config-FrontmanAiVite Not a pure module */ export { Config, Middleware, Plugin, SSE, Server, ToolRegistry, createMiddleware, frontmanPlugin, makeConfig };