fix: cubic comments and injected block inputs unrolling

This commit is contained in:
Dallin Romney 2025-10-15 15:10:54 -07:00
parent e53661f893
commit cdf770a1a4
5 changed files with 741 additions and 18 deletions

View file

@ -302,7 +302,10 @@ export function isStringRule(rule: string) {
if (rule.includes(" ") || rule.includes("\n")) {
return true;
}
if (["file:/", ".", "/", "~"].some((prefix) => rule.startsWith(prefix))) {
if (
["file:/", ".", "/", "~"].some((prefix) => rule.startsWith(prefix)) ||
rule.includes("\\")
) {
return false;
}
if (HUB_SLUG_PATTERN.test(rule)) {

View file

@ -0,0 +1,538 @@
import { Registry } from "../interfaces/index.js";
import { PackageIdentifier } from "../interfaces/slugs.js";
import { ConfigYaml } from "../schemas/index.js";
import { unrollBlocks } from "./unroll.js";
// Mock Registry for testing
class MockRegistry implements Registry {
private content: Record<string, string> = {};
setContent(id: string, content: string) {
this.content[id] = content;
}
async getContent(id: PackageIdentifier): Promise<string> {
const key =
id.uriType === "file"
? id.fileUri
: `${id.fullSlug.ownerSlug}/${id.fullSlug.packageSlug}`;
if (this.content[key]) {
return this.content[key];
}
throw new Error(`Content not found for ${key}`);
}
}
describe("injectBlocks with input-to-secret conversion", () => {
let mockRegistry: MockRegistry;
beforeEach(() => {
mockRegistry = new MockRegistry();
});
it("converts inputs to secrets in injected model blocks", async () => {
// Set up a basic assistant config
const assistant: ConfigYaml = {
name: "Test Assistant",
version: "1.0.0",
};
// Set up an injected block with input template variables in model
// Note: Block schema requires exactly 1 element in arrays
const injectedBlockContent = `
name: OpenAI Block
version: 1.0.0
schema: v1
models:
- name: gpt-4
provider: openai
model: gpt-4
apiKey: \${{ inputs.openaiKey }}
apiBase: \${{ inputs.customEndpoint }}
`;
const injectedBlockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/openai-block.yaml",
};
mockRegistry.setContent(
"file:///test/openai-block.yaml",
injectedBlockContent,
);
// Unroll with injected blocks
const result = await unrollBlocks(
assistant,
mockRegistry,
[injectedBlockId], // inject the block
undefined, // no allowlist
undefined, // no blocklist
undefined, // no request options
);
expect(result.config).toBeDefined();
expect(result.config!.models).toBeDefined();
expect(result.config!.models!.length).toBe(1);
// Check that inputs were converted to secrets in the model
const model = result.config!.models![0]!;
expect(model.apiKey).toBe("${{ secrets.openaiKey }}");
expect(model.apiBase).toBe("${{ secrets.customEndpoint }}");
expect(model.name).toBe("gpt-4");
expect(model.provider).toBe("openai");
expect(model.model).toBe("gpt-4");
// Check that no input variables remain
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.openaiKey");
expect(configStr).not.toContain("inputs.customEndpoint");
});
it("converts inputs to secrets in injected rule blocks", async () => {
const assistant: ConfigYaml = {
name: "Rule Test Assistant",
version: "1.0.0",
};
const injectedBlockContent = `
name: Style Rules Block
version: 1.0.0
schema: v1
rules:
- "Use \${{ inputs.codeStyle }} formatting consistently"
`;
const injectedBlockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/rules-block.yaml",
};
mockRegistry.setContent(
"file:///test/rules-block.yaml",
injectedBlockContent,
);
const result = await unrollBlocks(
assistant,
mockRegistry,
[injectedBlockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
expect(result.config!.rules).toBeDefined();
expect(result.config!.rules!.length).toBe(1);
// Check that input was converted to secret in the rule
// Note: string rules get wrapped in objects with sourceFile when injected
const rule = result.config!.rules![0]!;
if (typeof rule === "string") {
expect(rule).toBe("Use ${{ secrets.codeStyle }} formatting consistently");
} else {
expect(rule.rule).toBe(
"Use ${{ secrets.codeStyle }} formatting consistently",
);
}
// Check that no input variables remain
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.codeStyle");
});
it("converts inputs to secrets in injected docs blocks", async () => {
const assistant: ConfigYaml = {
name: "Docs Test Assistant",
version: "1.0.0",
};
const injectedBlockContent = `
name: Dynamic Docs Block
version: 1.0.0
schema: v1
docs:
- name: project-docs
startUrl: \${{ inputs.docsBaseUrl }}/api
rootUrl: \${{ inputs.docsBaseUrl }}
faviconUrl: \${{ inputs.docsBaseUrl }}/favicon.ico
`;
const injectedBlockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/docs-block.yaml",
};
mockRegistry.setContent(
"file:///test/docs-block.yaml",
injectedBlockContent,
);
const result = await unrollBlocks(
assistant,
mockRegistry,
[injectedBlockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
expect(result.config!.docs).toBeDefined();
expect(result.config!.docs!.length).toBe(1);
// Check that inputs were converted to secrets in the doc config
const doc = result.config!.docs![0]!;
expect(doc.startUrl).toBe("${{ secrets.docsBaseUrl }}/api");
expect(doc.rootUrl).toBe("${{ secrets.docsBaseUrl }}");
expect(doc.faviconUrl).toBe("${{ secrets.docsBaseUrl }}/favicon.ico");
expect(doc.name).toBe("project-docs");
// Check that no input variables remain
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.docsBaseUrl");
});
it("converts inputs to secrets in injected prompt blocks", async () => {
const assistant: ConfigYaml = {
name: "Prompt Test Assistant",
version: "1.0.0",
};
const injectedBlockContent = `
name: Dynamic Prompt Block
version: 1.0.0
schema: v1
prompts:
- name: custom-prompt
description: "A customizable prompt"
prompt: "You are a \${{ inputs.roleType }} assistant. Use \${{ inputs.responseStyle }} responses."
`;
const injectedBlockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/prompt-block.yaml",
};
mockRegistry.setContent(
"file:///test/prompt-block.yaml",
injectedBlockContent,
);
const result = await unrollBlocks(
assistant,
mockRegistry,
[injectedBlockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
expect(result.config!.prompts).toBeDefined();
expect(result.config!.prompts!.length).toBe(1);
// Check that inputs were converted to secrets in the prompt
const prompt = result.config!.prompts![0]!;
expect(prompt.prompt).toBe(
"You are a ${{ secrets.roleType }} assistant. Use ${{ secrets.responseStyle }} responses.",
);
expect(prompt.name).toBe("custom-prompt");
expect(prompt.description).toBe("A customizable prompt");
// Check that no input variables remain
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.roleType");
expect(configStr).not.toContain("inputs.responseStyle");
});
it("handles multiple injected blocks of different types", async () => {
const assistant: ConfigYaml = {
name: "Multi-Block Assistant",
version: "1.0.0",
};
// Model block with inputs
const modelBlockContent = `
name: API Model Block
version: 1.0.0
schema: v1
models:
- name: custom-api
provider: openai
model: \${{ inputs.modelName }}
apiKey: \${{ inputs.apiKey }}
`;
// Rules block with inputs
const rulesBlockContent = `
name: Dynamic Rules Block
version: 1.0.0
schema: v1
rules:
- "Follow \${{ inputs.codingStandard }} conventions"
`;
const modelBlockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/model-block.yaml",
};
const rulesBlockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/rules-block.yaml",
};
mockRegistry.setContent("file:///test/model-block.yaml", modelBlockContent);
mockRegistry.setContent("file:///test/rules-block.yaml", rulesBlockContent);
const result = await unrollBlocks(
assistant,
mockRegistry,
[modelBlockId, rulesBlockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
// Check model block conversion
expect(result.config!.models).toBeDefined();
expect(result.config!.models!.length).toBe(1);
const model = result.config!.models![0]!;
expect(model.model).toBe("${{ secrets.modelName }}");
expect(model.apiKey).toBe("${{ secrets.apiKey }}");
// Check rules block conversion
expect(result.config!.rules).toBeDefined();
expect(result.config!.rules!.length).toBe(1);
const rule = result.config!.rules![0]!;
if (typeof rule === "string") {
expect(rule).toBe("Follow ${{ secrets.codingStandard }} conventions");
} else {
expect(rule.rule).toBe(
"Follow ${{ secrets.codingStandard }} conventions",
);
}
// Verify no input variables remain
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.modelName");
expect(configStr).not.toContain("inputs.apiKey");
expect(configStr).not.toContain("inputs.codingStandard");
});
it("handles blocks with no template variables", async () => {
const assistant: ConfigYaml = {
name: "Static Assistant",
version: "1.0.0",
};
const staticBlockContent = `
name: Static Block
version: 1.0.0
schema: v1
models:
- name: static-model
provider: ollama
model: llama3.1
defaultCompletionOptions:
temperature: 0.5
stream: false
`;
const blockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/static-block.yaml",
};
mockRegistry.setContent(
"file:///test/static-block.yaml",
staticBlockContent,
);
const result = await unrollBlocks(
assistant,
mockRegistry,
[blockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
// Check that static content is preserved as-is
expect(result.config!.models).toBeDefined();
expect(result.config!.models!.length).toBe(1);
const model = result.config!.models![0]!;
expect(model.name).toBe("static-model");
expect(model.provider).toBe("ollama");
expect(model.model).toBe("llama3.1");
expect(model.defaultCompletionOptions?.temperature).toBe(0.5);
expect(model.defaultCompletionOptions?.stream).toBe(false);
});
it("preserves non-input template variables", async () => {
const assistant: ConfigYaml = {
name: "Mixed Variables Assistant",
version: "1.0.0",
};
const mixedBlockContent = `
name: Mixed Variables Block
version: 1.0.0
schema: v1
rules:
- "Input: \${{ inputs.userSetting }} | Secret: \${{ secrets.apiKey }} | Continue: \${{ continue.workspaceRoot }}"
`;
const blockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/mixed-block.yaml",
};
mockRegistry.setContent("file:///test/mixed-block.yaml", mixedBlockContent);
const result = await unrollBlocks(
assistant,
mockRegistry,
[blockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
expect(result.config!.rules).toBeDefined();
expect(result.config!.rules!.length).toBe(1);
const rule = result.config!.rules![0]!;
let ruleText: string;
if (typeof rule === "string") {
ruleText = rule;
} else {
ruleText = rule.rule;
}
// Input should be converted to secret, others should remain unchanged
expect(ruleText).toBe(
"Input: ${{ secrets.userSetting }} | Secret: ${{ secrets.apiKey }} | Continue: ${{ continue.workspaceRoot }}",
);
// Verify only inputs were converted
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.userSetting");
expect(configStr).toContain("secrets.apiKey");
expect(configStr).toContain("continue.workspaceRoot");
});
it("adds source file information to injected blocks", async () => {
const assistant: ConfigYaml = {
name: "Source Test Assistant",
version: "1.0.0",
};
const blockContent = `
name: Source Block
version: 1.0.0
schema: v1
rules:
- name: custom-rule
rule: "Always add type hints with \${{ inputs.typeStyle }}"
description: "Enforce type hints for better code clarity"
`;
const blockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/source-block.yaml",
};
mockRegistry.setContent("file:///test/source-block.yaml", blockContent);
const result = await unrollBlocks(
assistant,
mockRegistry,
[blockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
// Check that rules have source file information
expect(result.config!.rules).toBeDefined();
expect(result.config!.rules!.length).toBe(1);
const rule = result.config!.rules![0];
expect(rule).toMatchObject({
name: "custom-rule",
rule: "Always add type hints with ${{ secrets.typeStyle }}",
description: "Enforce type hints for better code clarity",
sourceFile: "file:///test/source-block.yaml",
});
// Verify input was converted
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.typeStyle");
});
it("handles complex nested input variables", async () => {
const assistant: ConfigYaml = {
name: "Nested Variables Assistant",
version: "1.0.0",
};
const nestedBlockContent = `
name: Nested Variables Block
version: 1.0.0
schema: v1
rules:
- "Database config: host=\${{ inputs.db.host }};port=\${{ inputs.db.port }};user=\${{ inputs.db.user }}"
`;
const blockId: PackageIdentifier = {
uriType: "file",
fileUri: "file:///test/nested-block.yaml",
};
mockRegistry.setContent(
"file:///test/nested-block.yaml",
nestedBlockContent,
);
const result = await unrollBlocks(
assistant,
mockRegistry,
[blockId],
undefined,
undefined,
undefined,
);
expect(result.config).toBeDefined();
expect(result.config!.rules).toBeDefined();
expect(result.config!.rules!.length).toBe(1);
const rule = result.config!.rules![0]!;
let ruleText: string;
if (typeof rule === "string") {
ruleText = rule;
} else {
ruleText = rule.rule;
}
// All nested inputs should be converted to secrets
expect(ruleText).toBe(
"Database config: host=${{ secrets.db.host }};port=${{ secrets.db.port }};user=${{ secrets.db.user }}",
);
// Verify all nested inputs were converted
const configStr = JSON.stringify(result.config);
expect(configStr).not.toContain("inputs.db.host");
expect(configStr).not.toContain("inputs.db.port");
expect(configStr).not.toContain("inputs.db.user");
});
});

View file

@ -1,5 +1,8 @@
import { PackageIdentifier } from "../interfaces/slugs.js";
import { parseMarkdownRuleOrAssistantUnrolled } from "./unroll.js";
import {
parseMarkdownRuleOrAssistantUnrolled,
replaceInputsWithSecrets,
} from "./unroll.js";
describe("parseMarkdownRuleOrAssistantUnrolled tests", () => {
it("parses valid YAML content as AssistantUnrolled", () => {
@ -88,3 +91,186 @@ model: # should be models
expect(rule).toHaveProperty("rule", dubiousContent);
});
});
describe("replaceInputsWithSecrets tests", () => {
it("replaces single input with secret", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
value: \${{ inputs.apiKey }}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.apiKey }}");
expect(result).not.toContain("\${{ inputs.apiKey }}");
});
it("replaces multiple inputs with secrets", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
apiKey: \${{ inputs.apiKey }}
dbPassword: \${{ inputs.dbPassword }}
endpoint: \${{ inputs.endpoint }}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.apiKey }}");
expect(result).toContain("\${{ secrets.dbPassword }}");
expect(result).toContain("\${{ secrets.endpoint }}");
expect(result).not.toContain("\${{ inputs.apiKey }}");
expect(result).not.toContain("\${{ inputs.dbPassword }}");
expect(result).not.toContain("\${{ inputs.endpoint }}");
});
it("leaves secrets template variables unchanged", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
apiKey: \${{ secrets.existingSecret }}
dbPassword: \${{ inputs.dbPassword }}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.existingSecret }}");
expect(result).toContain("\${{ secrets.dbPassword }}");
expect(result).not.toContain("\${{ inputs.dbPassword }}");
});
it("leaves other template variables unchanged", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
apiKey: \${{ inputs.apiKey }}
environment: \${{ continue.environment }}
customVar: \${{ other.variable }}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.apiKey }}");
expect(result).toContain("\${{ continue.environment }}");
expect(result).toContain("\${{ other.variable }}");
expect(result).not.toContain("\${{ inputs.apiKey }}");
});
it("handles yaml with no input template variables", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
staticValue: "hello world"
secretValue: \${{ secrets.mySecret }}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toBe(yamlContent); // Should remain unchanged
expect(result).toContain("\${{ secrets.mySecret }}");
});
it("handles empty string", () => {
const yamlContent = "";
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toBe("");
});
it("handles inputs with whitespace in template variables", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
value1: \${{ inputs.apiKey }}
value2: \${{\tinputs.tabbed\t}}
value3: \${{\n inputs.multiline \n}}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.apiKey }}");
expect(result).toContain("\${{ secrets.tabbed }}");
expect(result).toContain("\${{ secrets.multiline }}");
expect(result).not.toContain("inputs.apiKey");
expect(result).not.toContain("inputs.tabbed");
expect(result).not.toContain("inputs.multiline");
});
it("handles nested input keys", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
config: \${{ inputs.database.host }}
port: \${{ inputs.database.port }}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.database.host }}");
expect(result).toContain("\${{ secrets.database.port }}");
expect(result).not.toContain("inputs.database.host");
expect(result).not.toContain("inputs.database.port");
});
it("handles multiple inputs on same line", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
connectionString: "host=\${{ inputs.host }};port=\${{ inputs.port }};user=\${{ inputs.user }}"
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.host }}");
expect(result).toContain("\${{ secrets.port }}");
expect(result).toContain("\${{ secrets.user }}");
expect(result).not.toContain("inputs.host");
expect(result).not.toContain("inputs.port");
expect(result).not.toContain("inputs.user");
});
it("preserves malformed template variables that don't start with inputs", () => {
const yamlContent = `
name: Test
version: 1.0.0
data:
value1: \${{ inputs.valid }}
value2: \${{ malformed.inputs.something }}
value3: \${{ input.typo }}
`;
const result = replaceInputsWithSecrets(yamlContent);
expect(result).toContain("\${{ secrets.valid }}");
expect(result).toContain("\${{ malformed.inputs.something }}"); // Should remain unchanged
expect(result).toContain("\${{ input.typo }}"); // Should remain unchanged (typo in 'input')
expect(result).not.toContain("inputs.valid");
});
it("handles YAML with complex structure", () => {
const yamlContent = `
name: Complex Test
version: 1.0.0
models:
- name: gpt-4
apiKey: \${{ inputs.openaiKey }}
prompts:
- name: system
content: "You are a helpful assistant. API key: \${{ inputs.openaiKey }}"
rules:
- "Use \${{ inputs.style }} formatting"
data:
config:
database:
host: \${{ inputs.dbHost }}
password: \${{ inputs.dbPassword }}
external:
secret: \${{ secrets.external }}
other: \${{ other.variable }}
`;
const result = replaceInputsWithSecrets(yamlContent);
// Check all inputs were replaced with secrets
expect(result).toContain("\${{ secrets.openaiKey }}");
expect(result).toContain("\${{ secrets.style }}");
expect(result).toContain("\${{ secrets.dbHost }}");
expect(result).toContain("\${{ secrets.dbPassword }}");
// Check that non-input variables remain unchanged
expect(result).toContain("\${{ secrets.external }}");
expect(result).toContain("\${{ other.variable }}");
// Check that no input variables remain
expect(result).not.toContain("inputs.openaiKey");
expect(result).not.toContain("inputs.style");
expect(result).not.toContain("inputs.dbHost");
expect(result).not.toContain("inputs.dbPassword");
});
});

View file

@ -240,13 +240,15 @@ export async function unrollAssistant(
}
export function replaceInputsWithSecrets(yamlContent: string): string {
const inputsToSecrets = Object.fromEntries(
getTemplateVariables(yamlContent)
.filter((v) => v.startsWith("inputs."))
.map((v) => [v, `\${{ ${v.replace("inputs.", "secrets.")} }}`]),
);
const inputsToSecretsMap: Record<string, string> = {};
return renderTemplateData(yamlContent, { secrets: inputsToSecrets });
getTemplateVariables(yamlContent)
.filter((v) => v.startsWith("inputs."))
.forEach((v) => {
inputsToSecretsMap[v] = `\${{ ${v.replace("inputs.", "secrets.")} }}`;
});
return fillTemplateVariables(yamlContent, inputsToSecretsMap);
}
function renderTemplateData(
@ -539,19 +541,13 @@ export async function unrollBlocks(
const injectedBlockPromises = injectBlocks.map(async (injectBlock) => {
try {
const blockConfigYaml = await registry.getContent(injectBlock);
// Replace inputs with secrets for injected blocks since they can't render inputs
const blockConfigYamlWithSecrets =
replaceInputsWithSecrets(blockConfigYaml);
const parsedBlock = parseMarkdownRuleOrConfigYaml(
const resolvedBlock = parseMarkdownRuleOrConfigYaml(
blockConfigYamlWithSecrets,
injectBlock,
);
const blockType = getBlockType(parsedBlock);
const resolvedBlock = await resolveBlock(
injectBlock,
undefined,
registry,
);
const blockType = getBlockType(resolvedBlock);
return {
blockType,

View file

@ -157,6 +157,6 @@ export function parseAgentFileTools(toolsString?: string): ParsedAgentTools {
export function parseAgentFileRules(rules: string) {
return rules
.split(",")
.filter(Boolean)
.map((r) => r.trim());
.map((r) => r.trim())
.filter(Boolean);
}