add asConfigResult field to BaseUnrollAssistantOptions
This commit is contained in:
parent
f7fc1b83f2
commit
cdcb35484d
1 changed files with 46 additions and 88 deletions
|
|
@ -17,7 +17,7 @@ import {
|
|||
configYamlSchema,
|
||||
Rule,
|
||||
} from "../schemas/index.js";
|
||||
import { ConfigResult } from "../validation.js";
|
||||
import { ConfigResult, ConfigValidationError } from "../validation.js";
|
||||
import {
|
||||
packageIdentifierToShorthandSlug,
|
||||
useProxyForUnrenderedSecrets,
|
||||
|
|
@ -184,6 +184,7 @@ async function extractRenderedSecretsMap(
|
|||
export interface BaseUnrollAssistantOptions {
|
||||
renderSecrets: boolean;
|
||||
injectBlocks?: PackageIdentifier[];
|
||||
asConfigResult?: true;
|
||||
}
|
||||
|
||||
export interface DoNotRenderSecretsUnrollAssistantOptions
|
||||
|
|
@ -205,49 +206,35 @@ export type UnrollAssistantOptions =
|
|||
| DoNotRenderSecretsUnrollAssistantOptions
|
||||
| RenderSecretsUnrollAssistantOptions;
|
||||
|
||||
// Overload when we need error propapagation.
|
||||
// Overload to satisfy existing consumers of unrollAssistant.
|
||||
export async function unrollAssistant(
|
||||
id: PackageIdentifier,
|
||||
registry: Registry,
|
||||
options: UnrollAssistantOptions,
|
||||
needErrorPropagation: true,
|
||||
options: UnrollAssistantOptions & { asConfigResult: true }
|
||||
): Promise<ConfigResult<AssistantUnrolled>>;
|
||||
|
||||
// Overload when we don't need error propapagation.
|
||||
export async function unrollAssistant(
|
||||
id: PackageIdentifier,
|
||||
registry: Registry,
|
||||
options: UnrollAssistantOptions,
|
||||
needErrorPropagation?: false,
|
||||
options: UnrollAssistantOptions
|
||||
): Promise<AssistantUnrolled>;
|
||||
|
||||
export async function unrollAssistant(
|
||||
id: PackageIdentifier,
|
||||
registry: Registry,
|
||||
options: UnrollAssistantOptions,
|
||||
needErrorPropagation: boolean = false,
|
||||
): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> {
|
||||
// Request the content from the registry
|
||||
const rawContent = await registry.getContent(id);
|
||||
|
||||
// Somewhat annoyingly, we need this redundant ternary to circumvent overload matching error.
|
||||
// Without it TypeScript complains that no overloads of unrollAssistant
|
||||
// has a field needErrorPropagation that accepts a boolean.
|
||||
return needErrorPropagation
|
||||
? unrollAssistantFromContent(
|
||||
id,
|
||||
rawContent,
|
||||
registry,
|
||||
options,
|
||||
needErrorPropagation,
|
||||
)
|
||||
: unrollAssistantFromContent(
|
||||
id,
|
||||
rawContent,
|
||||
registry,
|
||||
options,
|
||||
needErrorPropagation,
|
||||
);
|
||||
const result = unrollAssistantFromContent(
|
||||
id,
|
||||
rawContent,
|
||||
registry,
|
||||
options,
|
||||
)
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function renderTemplateData(
|
||||
|
|
@ -267,30 +254,11 @@ function renderTemplateData(
|
|||
return templatedYaml;
|
||||
}
|
||||
|
||||
// Overload when we need error propapagation.
|
||||
export async function unrollAssistantFromContent(
|
||||
id: PackageIdentifier,
|
||||
rawYaml: string,
|
||||
registry: Registry,
|
||||
options: UnrollAssistantOptions,
|
||||
needErrorPropagation: true,
|
||||
): Promise<ConfigResult<AssistantUnrolled>>;
|
||||
|
||||
// Overload when we need don't error propapagation.
|
||||
export async function unrollAssistantFromContent(
|
||||
id: PackageIdentifier,
|
||||
rawYaml: string,
|
||||
registry: Registry,
|
||||
options: UnrollAssistantOptions,
|
||||
needErrorPropagation?: false,
|
||||
): Promise<AssistantUnrolled>;
|
||||
|
||||
export async function unrollAssistantFromContent(
|
||||
id: PackageIdentifier,
|
||||
rawYaml: string,
|
||||
registry: Registry,
|
||||
options: UnrollAssistantOptions,
|
||||
needErrorPropagation: boolean = false,
|
||||
): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> {
|
||||
// Parse string to Zod-validated YAML
|
||||
let parsedYaml = parseConfigYaml(rawYaml);
|
||||
|
|
@ -300,15 +268,16 @@ export async function unrollAssistantFromContent(
|
|||
parsedYaml,
|
||||
registry,
|
||||
options.injectBlocks,
|
||||
needErrorPropagation,
|
||||
options.asConfigResult ?? false
|
||||
);
|
||||
|
||||
// Back to a string so we can fill in template variables
|
||||
const rawUnrolledYaml = needErrorPropagation
|
||||
? YAML.stringify(
|
||||
(unrolledAssistant as ConfigResult<AssistantUnrolled>).config,
|
||||
)
|
||||
: YAML.stringify(unrolledAssistant);
|
||||
const rawUnrolledYaml = YAML.stringify(unrolledAssistant);
|
||||
// const rawUnrolledYaml = needErrorPropagation
|
||||
// ? YAML.stringify(
|
||||
// (unrolledAssistant as ConfigResult<AssistantUnrolled>).config,
|
||||
// )
|
||||
// : YAML.stringify(unrolledAssistant);
|
||||
|
||||
// Convert all of the template variables to FQSNs
|
||||
// Secrets from the block will have the assistant slug prepended to the FQSN
|
||||
|
|
@ -338,7 +307,7 @@ export async function unrollAssistantFromContent(
|
|||
options.onPremProxyUrl,
|
||||
);
|
||||
|
||||
if (needErrorPropagation) {
|
||||
if (options.asConfigResult) {
|
||||
return {
|
||||
config: finalConfig,
|
||||
errors: (unrolledAssistant as ConfigResult<AssistantUnrolled>).errors,
|
||||
|
|
@ -351,22 +320,13 @@ export async function unrollAssistantFromContent(
|
|||
return finalConfig;
|
||||
}
|
||||
|
||||
// We don't need oveloads here because
|
||||
// the only place unrollBlocks is called directly is this file.
|
||||
// unrollAssistantFromContent and unrollAssistant
|
||||
// are called elsewhere in this project
|
||||
// and imported in other projects.
|
||||
export async function unrollBlocks(
|
||||
assistant: ConfigYaml,
|
||||
registry: Registry,
|
||||
injectBlocks: PackageIdentifier[] | undefined,
|
||||
needErrorPropagation: boolean = false,
|
||||
asConfigError: boolean
|
||||
): Promise<AssistantUnrolled | ConfigResult<AssistantUnrolled>> {
|
||||
const configResult: ConfigResult<AssistantUnrolled> = {
|
||||
config: undefined,
|
||||
errors: [],
|
||||
configLoadInterrupted: false,
|
||||
};
|
||||
const errors: ConfigValidationError[] = [];
|
||||
|
||||
const unrolledAssistant: AssistantUnrolled = {
|
||||
name: assistant.name,
|
||||
|
|
@ -399,13 +359,11 @@ export async function unrollBlocks(
|
|||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (needErrorPropagation) {
|
||||
configResult.errors?.push({
|
||||
fatal: false,
|
||||
// Using JSON.stringify to avoid printing [object Object].
|
||||
message: `Failed to unroll block ${JSON.stringify(unrolledBlock.uses)}: ${(err as Error).message}`,
|
||||
});
|
||||
}
|
||||
errors.push({
|
||||
fatal: false,
|
||||
// Using JSON.stringify to avoid printing [object Object].
|
||||
message: `${(err as Error).message}:\n${JSON.stringify(unrolledBlock.uses)}`,
|
||||
});
|
||||
|
||||
console.error(
|
||||
`Failed to unroll block ${JSON.stringify(unrolledBlock.uses)}: ${(err as Error).message}`,
|
||||
|
|
@ -440,13 +398,11 @@ export async function unrollBlocks(
|
|||
rules.push(block);
|
||||
}
|
||||
} catch (err) {
|
||||
if (needErrorPropagation) {
|
||||
configResult.errors?.push({
|
||||
fatal: false,
|
||||
// Using JSON.stringify to avoid printing [object Object].
|
||||
message: `Failed to unroll block ${JSON.stringify(rule.uses)}: ${(err as Error).message}`,
|
||||
});
|
||||
}
|
||||
errors.push({
|
||||
fatal: false,
|
||||
// Using JSON.stringify to avoid printing [object Object].
|
||||
message: `${(err as Error).message}:\n${JSON.stringify(rule.uses)}`,
|
||||
});
|
||||
|
||||
console.error(
|
||||
`Failed to unroll block ${JSON.stringify(rule.uses)}: ${(err as Error).message}`,
|
||||
|
|
@ -480,13 +436,11 @@ export async function unrollBlocks(
|
|||
);
|
||||
}
|
||||
} catch (err) {
|
||||
if (needErrorPropagation) {
|
||||
configResult.errors?.push({
|
||||
fatal: false,
|
||||
// Using JSON.stringify to avoid printing [object Object].
|
||||
message: `Failed to unroll block ${JSON.stringify(injectBlock)}: ${(err as Error).message}`,
|
||||
});
|
||||
}
|
||||
errors.push({
|
||||
fatal: false,
|
||||
// Using JSON.stringify to avoid printing [object Object].
|
||||
message: `${(err as Error).message}:\n${JSON.stringify(injectBlock)}`,
|
||||
});
|
||||
|
||||
console.error(
|
||||
`Failed to unroll block ${JSON.stringify(injectBlock)}: ${(err as Error).message}`,
|
||||
|
|
@ -494,12 +448,16 @@ export async function unrollBlocks(
|
|||
}
|
||||
}
|
||||
|
||||
if (needErrorPropagation) {
|
||||
if (asConfigError) {
|
||||
const configResult: ConfigResult<AssistantUnrolled> = {
|
||||
config: undefined,
|
||||
errors: undefined,
|
||||
configLoadInterrupted: false,
|
||||
};
|
||||
configResult.config = unrolledAssistant;
|
||||
if (configResult.errors?.length == 0) {
|
||||
configResult.errors = undefined;
|
||||
if (errors.length > 0) {
|
||||
configResult.errors = errors;
|
||||
}
|
||||
configResult.configLoadInterrupted = false;
|
||||
return configResult;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue