diff --git a/packages/config-yaml/src/load/unroll.test.ts b/packages/config-yaml/src/load/unroll.test.ts new file mode 100644 index 000000000..83bec0cd0 --- /dev/null +++ b/packages/config-yaml/src/load/unroll.test.ts @@ -0,0 +1,91 @@ +import { PackageIdentifier } from "../interfaces/slugs.js"; +import { parseMarkdownRuleOrAssistantUnrolled } from "./unroll.js"; + +describe("parseMarkdownRuleOrAssistantUnrolled tests", () => { + it("parses valid YAML content as AssistantUnrolled", () => { + const mockId: PackageIdentifier = { + uriType: "file", + filePath: "./foo/bar.yaml", + }; + const yamlContent = ` +name: Test Assistant +version: 1.0.0 + +models: + - name: model + model: modelname + provider: ollama +`; + const result = parseMarkdownRuleOrAssistantUnrolled(yamlContent, mockId); + expect(result).toHaveProperty("name", "Test Assistant"); + expect(result).toHaveProperty("version", "1.0.0"); + expect(result.models?.length).toBe(1); + expect(result.models?.[0]).toHaveProperty("name", "model"); + }); + + it("parses markdown rule content as AssistantUnrolled", () => { + const mockId: PackageIdentifier = { + uriType: "file", + filePath: "./foo/bar.md", + }; + const markdownContent = ` +--- +name: rulename +description: my rule description +pattern: "**" +--- +This is the rule +`; + const result = parseMarkdownRuleOrAssistantUnrolled( + markdownContent, + mockId, + ); + expect(result).toHaveProperty("name"); + expect(result).toHaveProperty("version"); + expect(Array.isArray(result.rules)).toBe(true); + expect(result.rules).toBeDefined(); + expect(result.rules!.length).toBe(1); + const rule = result.rules![0]; + expect(rule).toHaveProperty("name", "rulename"); + expect(rule).toHaveProperty("description", "my rule description"); + expect(rule).toHaveProperty("globs", "./foo/**/*"); + expect(rule).toHaveProperty("rule", "This is the rule"); + }); + + it("throws error for invalid content in yaml file", () => { + const mockId: PackageIdentifier = { + uriType: "file", + filePath: "./foo/bar.yaml", + }; + const invalidContent = ` +name: Test Assistant +version: 1.0.0 + +model: # should be models + - name: model + model: modelname + provider: ollama +`; + expect(() => + parseMarkdownRuleOrAssistantUnrolled(invalidContent, mockId), + ).toThrow(); + }); + + it("Every non-YAML file is a rule", () => { + const mockId: PackageIdentifier = { + uriType: "file", + filePath: "./foo/bar", + }; + const dubiousContent = `This is not \nproper #YAML#`; + const result = parseMarkdownRuleOrAssistantUnrolled(dubiousContent, mockId); + expect(result).toHaveProperty("name"); + expect(result).toHaveProperty("version"); + expect(Array.isArray(result.rules)).toBe(true); + expect(result.rules).toBeDefined(); + expect(result.rules!.length).toBe(1); + const rule = result.rules![0]; + expect(rule).toHaveProperty("name", "foo/bar"); + expect(rule).toHaveProperty("globs", "./foo/**/*"); + expect(rule).toHaveProperty("rule", dubiousContent); + }); +}); diff --git a/packages/config-yaml/src/load/unroll.ts b/packages/config-yaml/src/load/unroll.ts index 7802be705..80403b544 100644 --- a/packages/config-yaml/src/load/unroll.ts +++ b/packages/config-yaml/src/load/unroll.ts @@ -286,9 +286,7 @@ export async function unrollAssistantFromContent( options.platformClient, options.alwaysUseProxy, ); - const renderedYaml = renderTemplateData(templatedYaml, { - secrets, - }); + const renderedYaml = renderTemplateData(templatedYaml, { secrets }); // Parse again and replace models with proxy versions where secrets weren't rendered const finalConfig = useProxyForUnrenderedSecrets( @@ -298,11 +296,7 @@ export async function unrollAssistantFromContent( options.onPremProxyUrl, ); - return { - config: finalConfig, - errors, - configLoadInterrupted, - }; + return { config: finalConfig, errors, configLoadInterrupted }; } export async function unrollBlocks( @@ -366,10 +360,7 @@ export async function unrollBlocks( return { index, block: null, - error: { - fatal: false, - message: msg, - }, + error: { fatal: false, message: msg }, }; } } else { @@ -461,11 +452,7 @@ export async function unrollBlocks( registry, ); - return { - blockType, - resolvedBlock, - error: null, - }; + return { blockType, resolvedBlock, error: null }; } catch (err) { let msg = ""; if (injectBlock.uriType === "file") { @@ -481,10 +468,7 @@ export async function unrollBlocks( return { blockType: null, resolvedBlock: null, - error: { - fatal: false, - message: msg, - }, + error: { fatal: false, message: msg }, }; } }); @@ -581,57 +565,46 @@ export async function resolveBlock( return parseMarkdownRuleOrAssistantUnrolled(templatedYaml, id); } -function parseMarkdownRuleOrAssistantUnrolled( +export function parseMarkdownRuleOrAssistantUnrolled( content: string, id: PackageIdentifier, ): AssistantUnrolled { - // Try to parse as YAML first, then as markdown rule if that fails - let parsedYaml: AssistantUnrolled; - try { - parsedYaml = parseBlock(content); - } catch (yamlError) { - // If YAML parsing fails, try parsing as markdown rule - try { - const rule = markdownToRule(content, id); - // Convert the rule object to the expected format - parsedYaml = { - name: rule.name, - version: "1.0.0", - rules: [rule], - }; - } catch (markdownError) { - // If both fail, throw the original YAML error - throw yamlError; - } - } - - return parsedYaml; + return parseYamlOrMarkdownRule(content, id, parseBlock); } function parseMarkdownRuleOrConfigYaml( content: string, id: PackageIdentifier, ): ConfigYaml { - // Try to parse as YAML first, then as markdown rule if that fails - let parsedYaml: ConfigYaml; + return parseYamlOrMarkdownRule(content, id, parseConfigYaml); +} + +function parseYamlOrMarkdownRule( + content: string, + id: PackageIdentifier, + parseYamlFn: (content: string) => T, +): T { + let parsedYaml: T; try { - parsedYaml = parseConfigYaml(content); + // Try to parse as YAML first, then as markdown rule if that fails + parsedYaml = parseYamlFn(content); } catch (yamlError) { + if ( + id.uriType === "file" && + [".yaml", ".yml"].some((ext) => id.filePath.endsWith(ext)) + ) { + throw yamlError; + } // If YAML parsing fails, try parsing as markdown rule try { const rule = markdownToRule(content, id); // Convert the rule object to the expected format - parsedYaml = { - name: rule.name, - version: "1.0.0", - rules: [rule], - }; + parsedYaml = { name: rule.name, version: "1.0.0", rules: [rule] } as T; } catch (markdownError) { // If both fail, throw the original YAML error throw yamlError; } } - return parsedYaml; }