import type { ToolSetup } from './tool.ts' import type { AgentsTool } from './types.ts' import { createHash } from 'node:crypto' import { MAZ_UI_AGENTS_TOOL, resolveTool } from './tool.ts' import { compareVersions } from './utils.ts' /** Adds the note (version-free by default) under the title of a generated rule */ export function withVersion(content: string, version: string, tool: AgentsTool): string { if (tool.versionNote === false) return content const note = tool.versionNote?.(version) ?? `> These rules match the installed ${tool.name}.` return content.replace(/^(#{1,2} .+)\n/, (_, title: string) => `${title}\n\n${note}\n`) } /** Checksum of generated rules, insensitive to the whitespace a formatter changes (table padding, wrapping, blank lines) */ function checksum(body: string): string { return createHash('sha256').update(body.replaceAll(/\s+/g, '')).digest('hex').slice(0, 12) } /** Checksum of a frontmatter and a body: the quotes of the frontmatter are left out, a formatter can change them */ function ruleChecksum(frontmatter: string, body: string): string { return checksum(frontmatter.replaceAll(/["']/g, '') + body) } /** * Generated rules: the frontmatter (it must be the first thing of the file), a header with the version of the tool * that last changed the rules (a release that does not change them keeps the version already written) * and the checksum of the frontmatter and of the body, then the body */ export function managedContent(version: string, body: string, setup: ToolSetup, frontmatter = ''): string { const recorded = ruleChecksum(frontmatter, body) return `${frontmatter}\n\n${body}` } /** * `true` when generated rules were edited by hand since they were written: * they are kept, a newer version of the rules would overwrite the edits. * The checksum covers what follows the managed header, and also what precedes * it (the `paths:` frontmatter), so an edit of the frontmatter is kept too. A * checksum of the body alone is still accepted: rules written without * frontmatter (header on the first line), or before the frontmatter was part * of the checksum, are checked against it. */ export function isEditedManagedContent(content: string | undefined, tool: AgentsTool = MAZ_UI_AGENTS_TOOL): boolean { const header = content === undefined ? null : resolveTool(tool).managedHeader.exec(content) const recorded = header ? /\(sha256:([\da-f]+)\)/.exec(header[0])?.[1] : undefined if (header === null || recorded === undefined) return false const before = (content as string).slice(0, header.index) const after = (content as string).slice(header.index + header[0].length) return recorded !== checksum(after) && recorded !== ruleChecksum(before, after) } /** Version of the tool (maz-ui by default) that generated a managed content, read from its header */ export function readManagedVersion(content: string | undefined, tool: AgentsTool = MAZ_UI_AGENTS_TOOL): string | undefined { return content ? resolveTool(tool).managedVersion.exec(content)?.[1] : undefined } /** * `true` when a managed content already holds the rules about to be written (same recorded checksum): it is kept * as is, with the version of the tool that last changed the rules, instead of being rewritten at every release. */ export function hasSameManagedRules(current: string | undefined, next: string, tool: AgentsTool): boolean { const { managedHeader } = resolveTool(tool) const sha = (content: string | undefined): string | undefined => { const header = content === undefined ? null : managedHeader.exec(content) return header ? /\(sha256:([\da-f]+)\)/.exec(header[0])?.[1] : undefined } const recorded = sha(current) return recorded !== undefined && recorded === sha(next) } /** * Rules written by a newer version of the tool are kept: in a monorepo whose * apps install different versions, each app would otherwise rewrite them in turn. */ export function newerManagedVersion(content: string | undefined, version: string, tool: AgentsTool): string | undefined { const written = readManagedVersion(content, tool) return written && version && compareVersions(written, version) > 0 ? written : undefined } export function newerVersionReason(written: string, version: string, tool: AgentsTool): string { return `written by ${tool.name} ${written}, newer than the installed ${version}: left untouched` } /** A file we generated (or no file): we may overwrite or delete it */ export function isOwned(content: string | undefined, setup: ToolSetup): boolean { return content === undefined || content.includes(setup.marker) }