CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes45downloads
markdown-command-parser.ts74 linesDownload Raw Back to services
1/**2 * @license3 * Copyright 2025 Google LLC4 * SPDX-License-Identifier: Apache-2.05 */6 7import { z } from 'zod';8import {9  parse as parseYaml,10  normalizeContent,11} from '@qwen-code/qwen-code-core';12 13/**14 * Defines the Zod schema for a Markdown command definition file.15 * The frontmatter contains optional metadata, and the body is the prompt.16 */17export const MarkdownCommandDefSchema = z.object({18  frontmatter: z19    .object({20      description: z.string().optional(),21      'argument-hint': z.string().optional(),22      when_to_use: z.string().optional(),23      'disable-model-invocation': z.boolean().optional(),24    })25    .passthrough()26    .optional(),27  prompt: z.string({28    required_error: 'The prompt content is required.',29    invalid_type_error: 'The prompt content must be a string.',30  }),31});32 33export type MarkdownCommandDef = z.infer<typeof MarkdownCommandDefSchema>;34 35/**36 * Parses a Markdown command file with optional YAML frontmatter.37 * @param content The file content38 * @returns Parsed command definition with frontmatter and prompt39 */40export function parseMarkdownCommand(content: string): MarkdownCommandDef {41  const normalizedContent = normalizeContent(content);42 43  // Match YAML frontmatter pattern: ---\n...\n---\n44  // Allow empty frontmatter: ---\n---\n45  const frontmatterRegex = /^---\n(?:([\s\S]*?)\n)?---(?:\n|$)([\s\S]*)$/;46  const match = normalizedContent.match(frontmatterRegex);47 48  if (!match) {49    // No frontmatter, entire content is the prompt50    return {51      prompt: normalizedContent.trim(),52    };53  }54 55  const [, frontmatterYaml = '', body] = match;56 57  // Parse YAML frontmatter if not empty58  let frontmatter: Record<string, unknown> | undefined;59  if (frontmatterYaml.trim()) {60    try {61      frontmatter = parseYaml(frontmatterYaml) as Record<string, unknown>;62    } catch (error) {63      throw new Error(64        `Failed to parse YAML frontmatter: ${error instanceof Error ? error.message : String(error)}`,65      );66    }67  }68 69  return {70    frontmatter,71    prompt: body.trim(),72  };73}74 
basant307/AI_Governance_Project · CoolFace