UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

68 lines • 3.37 kB
/** * An Assembler `init` hook (added by `node ace configure @adonis-agora/agent`) that writes the * sandbox kit's docs — its components and their props, the styles' Tailwind tokens or CSS class * vocabulary, the theme's variables — on `serve` and `build`, and again when a kit or style file * changes. For an app whose Vite does not run in the server's process (a separate SPA): with Vite * in-process, `genuiSandboxKit()` does this itself. Does nothing when the project has no kit * (no `components/ui`, no `components`, `wrapper` or `styles`). * * ```ts * // adonisrc.ts * hooks: { init: [() => import('@adonis-agora/agent/hooks/genui_kit')] } * // or, with options: * import { genuiKitHook } from '@adonis-agora/agent/hooks/genui_kit' * hooks: { init: [genuiKitHook({ components: 'web/src/components/ui/*.tsx', styles: 'web/src/app.css' })] } * ``` */ import { relative, resolve } from 'node:path'; import { assertNoLegacyKitOptions, resolveSandboxKitFiles } from '../genui/kit/files.js'; import { sandboxKitDescriptorPath } from '../genui/kit/plugin.js'; import { resolveStyleEntries, STYLE_EXTENSIONS } from '../genui/kit/styles.js'; import { writeSandboxKitDocs } from '../genui/kit/write.js'; import { ADONIS_SANDBOX_KIT_DESCRIPTOR } from '../vite/index.js'; export function genuiKitHook(options = {}) { assertNoLegacyKitOptions(options, 'genuiKitHook'); const output = options.output ?? sandboxKitDescriptorPath(ADONIS_SANDBOX_KIT_DESCRIPTOR, options.name); const generate = async (root) => { try { await writeSandboxKitDocs({ ...options, root, output }); } catch (error) { console.warn(`[@adonis-agora/agent] sandbox kit docs not generated: ${error instanceof Error ? error.message : String(error)}`); } }; return { async run(parent, hooks) { const cwd = parent?.cwd; const root = cwd === undefined ? process.cwd() : typeof cwd === 'string' ? cwd : decodeURIComponent(cwd.pathname); let files = null; try { files = resolveSandboxKitFiles(root, options); } catch (error) { console.warn(`[@adonis-agora/agent] ${error instanceof Error ? error.message : String(error)}`); } const styles = options.styles === undefined ? [] : resolveStyleEntries(root, options.styles).files; if (files === null && styles.length === 0) return; await generate(root); const onChange = (_relative, absolute) => { const path = typeof absolute === 'string' ? absolute : resolve(root, String(_relative)); const inKit = files?.dirs.some((dir) => !relative(dir, path).startsWith('..')) === true; const isStyle = STYLE_EXTENSIONS.some((extension) => path.endsWith(extension)); if (inKit || isStyle) void generate(root); }; hooks?.add('fileChanged', onChange); hooks?.add('fileAdded', onChange); hooks?.add('fileRemoved', onChange); }, }; } /** `() => import('@adonis-agora/agent/hooks/genui_kit')` resolves to a ready hook. */ export default genuiKitHook(); //# sourceMappingURL=genui_kit.js.map