Merge branch 'nate/inject-blocks' into pe/assistant-unroll-bugfix

This commit is contained in:
Nate 2025-04-04 12:54:13 -07:00
commit 451d7ef780
2 changed files with 79 additions and 4 deletions

View file

@ -93,7 +93,7 @@ describe("E2E Scenarios", () => {
getContent: async function (fullSlug: FullSlug): Promise<string> {
return fs
.readFileSync(
`./test/packages/${fullSlug.ownerSlug}/${fullSlug.packageSlug}.yaml`,
`./src/__tests__/packages/${fullSlug.ownerSlug}/${fullSlug.packageSlug}.yaml`,
)
.toString();
},
@ -193,5 +193,40 @@ describe("E2E Scenarios", () => {
expect(geminiSecretValue2).toBe("gemini-api-key");
});
it("should correctly unroll assistant with injected blocks", async () => {
const unrolledConfig = await unrollAssistant(
"test-org/assistant",
registry,
{
renderSecrets: true,
platformClient,
orgScopeId: "test-org",
currentUserSlug: "test-user",
onPremProxyUrl: null,
// Add an injected block
injectBlocks: [
{
ownerSlug: "test-org",
packageSlug: "docs",
versionSlug: "latest",
},
],
},
);
// The original docs array should have one item
expect(unrolledConfig.docs?.length).toBe(2); // Now 2 with the injected block
// Check the original doc is still there
expect(unrolledConfig.docs?.[0]?.startUrl).toBe(
"https://docs.python.org/release/3.13.1",
);
// Check the injected doc block was added
expect(unrolledConfig.docs?.[1]?.startUrl).toBe(
"https://docs.python.org/release/3.13.1",
);
});
it.skip("should prioritize org over user / package secrets", () => {});
});

View file

@ -20,6 +20,7 @@ import {
Rule,
} from "../schemas/index.js";
import { useProxyForUnrenderedSecrets } from "./clientRender.js";
import { getBlockType } from "./getBlockType.js";
export function parseConfigYaml(configYaml: string): ConfigYaml {
try {
@ -178,11 +179,18 @@ async function extractRenderedSecretsMap(
return map;
}
export interface DoNotRenderSecretsUnrollAssistantOptions {
export interface BaseUnrollAssistantOptions {
renderSecrets: boolean;
injectBlocks?: FullSlug[];
}
export interface DoNotRenderSecretsUnrollAssistantOptions
extends BaseUnrollAssistantOptions {
renderSecrets: false;
}
export interface RenderSecretsUnrollAssistantOptions {
export interface RenderSecretsUnrollAssistantOptions
extends BaseUnrollAssistantOptions {
renderSecrets: true;
orgScopeId: string | null;
currentUserSlug: string;
@ -239,7 +247,11 @@ export async function unrollAssistantFromContent(
let parsedYaml = parseConfigYaml(rawYaml);
// Unroll blocks and convert their secrets to FQSNs
const unrolledAssistant = await unrollBlocks(parsedYaml, registry);
const unrolledAssistant = await unrollBlocks(
parsedYaml,
registry,
options.injectBlocks,
);
// Back to a string so we can fill in template variables
const rawUnrolledYaml = YAML.stringify(unrolledAssistant);
@ -277,6 +289,7 @@ export async function unrollAssistantFromContent(
export async function unrollBlocks(
assistant: ConfigYaml,
registry: Registry,
injectBlocks: FullSlug[] | undefined,
): Promise<AssistantUnrolled> {
const unrolledAssistant: AssistantUnrolled = {
name: assistant.name,
@ -353,6 +366,33 @@ export async function unrollBlocks(
unrolledAssistant.rules = rules;
}
// Add injected blocks
for (const injectBlock of injectBlocks ?? []) {
try {
const blockConfigYaml = await registry.getContent(injectBlock);
const parsedBlock = parseConfigYaml(blockConfigYaml);
const blockType = getBlockType(parsedBlock);
const resolvedBlock = await resolveBlock(
injectBlock,
undefined,
registry,
);
if (blockType) {
if (!unrolledAssistant[blockType]) {
unrolledAssistant[blockType] = [];
}
unrolledAssistant[blockType]?.push(
...(resolvedBlock[blockType] as any),
);
}
} catch (err) {
console.error(
`Failed to unroll block ${injectBlock}: ${(err as Error).message}`,
);
}
}
return unrolledAssistant;
}