Changesets · Branches · Tags · Compare
| Changeset | c7e22ae5359c5b61d61eb3a1693a30e028d8ed7e64f6d4513eeec82017839770 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-07-08 12:41 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-07-08 12:41 -07:00 |
| Parents | 74b917bba7fc, c07b8bc14bd7 |
1 file changed
modified packages/config-yaml/src/load/unroll.ts +181 −84
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | packageIdentifierToShorthandSlug, |
| 25 | 25 | useProxyForUnrenderedSecrets, |
| 26 | 26 | } from "./clientRender.js"; |
| 27 | -import { getBlockType } from "./getBlockType.js"; | |
| 27 | +import { BlockType, getBlockType } from "./getBlockType.js"; | |
| 28 | 28 | |
| 29 | 29 | export function parseConfigYaml(configYaml: string): ConfigYaml { |
| 30 | 30 | try { |
| @@ -322,12 +322,15 @@ | ||
| 322 | 322 | "name" | "version" | "rules" | "schema" | "metadata" |
| 323 | 323 | >)[] = ["models", "context", "data", "mcpServers", "prompts", "docs"]; |
| 324 | 324 | |
| 325 | - // For each section, replace "uses/with" blocks with the real thing | |
| 326 | - for (const section of sections) { | |
| 327 | - if (assistant[section]) { | |
| 328 | - const sectionBlocks: any[] = []; | |
| 325 | + // Process all sections in parallel | |
| 326 | + const sectionPromises = sections.map(async (section) => { | |
| 327 | + if (!assistant[section]) { | |
| 328 | + return { section, blocks: null }; | |
| 329 | + } | |
| 329 | 330 | |
| 330 | - for (const unrolledBlock of assistant[section]) { | |
| 331 | + // Process all blocks in this section in parallel | |
| 332 | + const blockPromises = assistant[section].map( | |
| 333 | + async (unrolledBlock, index) => { | |
| 331 | 334 | // "uses/with" block |
| 332 | 335 | if ("uses" in unrolledBlock) { |
| 333 | 336 | try { |
| @@ -338,10 +341,13 @@ | ||
| 338 | 341 | ); |
| 339 | 342 | const block = blockConfigYaml[section]?.[0]; |
| 340 | 343 | if (block) { |
| 341 | - sectionBlocks.push( | |
| 342 | - mergeOverrides(block, unrolledBlock.override ?? {}), | |
| 343 | - ); | |
| 344 | + return { | |
| 345 | + index, | |
| 346 | + block: mergeOverrides(block, unrolledBlock.override ?? {}), | |
| 347 | + error: null, | |
| 348 | + }; | |
| 344 | 349 | } |
| 350 | + return { index, block: null, error: null }; | |
| 345 | 351 | } catch (err) { |
| 346 | 352 | let msg = ""; |
| 347 | 353 | if ( |
| @@ -353,99 +359,190 @@ | ||
| 353 | 359 | msg = `${(err as Error).message}.\n> ${JSON.stringify(unrolledBlock.uses)}`; |
| 354 | 360 | } |
| 355 | 361 | |
| 356 | - errors.push({ | |
| 357 | - fatal: false, | |
| 358 | - message: msg, | |
| 359 | - }); | |
| 360 | - | |
| 361 | 362 | console.error( |
| 362 | 363 | `Failed to unroll block ${JSON.stringify(unrolledBlock.uses)}: ${(err as Error).message}`, |
| 363 | 364 | ); |
| 364 | - sectionBlocks.push(null); | |
| 365 | + | |
| 366 | + return { | |
| 367 | + index, | |
| 368 | + block: null, | |
| 369 | + error: { | |
| 370 | + fatal: false, | |
| 371 | + message: msg, | |
| 372 | + }, | |
| 373 | + }; | |
| 365 | 374 | } |
| 366 | 375 | } else { |
| 367 | 376 | // Normal block |
| 368 | - sectionBlocks.push(unrolledBlock); | |
| 377 | + return { index, block: unrolledBlock, error: null }; | |
| 369 | 378 | } |
| 370 | - } | |
| 379 | + }, | |
| 380 | + ); | |
| 381 | + | |
| 382 | + const blockResults = await Promise.all(blockPromises); | |
| 383 | + | |
| 384 | + // Collect errors and maintain order | |
| 385 | + const sectionBlocks: any[] = []; | |
| 386 | + const sectionErrors: ConfigValidationError[] = []; | |
| 371 | 387 | |
| 372 | - unrolledAssistant[section] = sectionBlocks; | |
| 388 | + for (const result of blockResults) { | |
| 389 | + if (result.error) { | |
| 390 | + sectionErrors.push(result.error); | |
| 391 | + } | |
| 392 | + sectionBlocks[result.index] = result.block; | |
| 373 | 393 | } |
| 374 | - } | |
| 375 | 394 | |
| 376 | - // Rules are a bit different because they can be strings, so handle separately | |
| 377 | - if (assistant.rules) { | |
| 378 | - const rules: (Rule | null)[] = []; | |
| 379 | - for (const rule of assistant.rules) { | |
| 380 | - if (typeof rule === "string" || !("uses" in rule)) { | |
| 381 | - rules.push(rule); | |
| 382 | - } else if ("uses" in rule) { | |
| 383 | - try { | |
| 384 | - const blockConfigYaml = await resolveBlock( | |
| 385 | - decodePackageIdentifier(rule.uses), | |
| 386 | - rule.with, | |
| 387 | - registry, | |
| 388 | - ); | |
| 389 | - const block = blockConfigYaml.rules?.[0]; | |
| 390 | - if (block) { | |
| 391 | - rules.push(block); | |
| 395 | + return { section, blocks: sectionBlocks, errors: sectionErrors }; | |
| 396 | + }); | |
| 397 | + | |
| 398 | + // Process rules in parallel | |
| 399 | + const rulesPromise = assistant.rules | |
| 400 | + ? (async () => { | |
| 401 | + const rulePromises = assistant.rules!.map(async (rule, index) => { | |
| 402 | + if (typeof rule === "string" || !("uses" in rule)) { | |
| 403 | + return { index, rule, error: null }; | |
| 404 | + } else if ("uses" in rule) { | |
| 405 | + try { | |
| 406 | + const blockConfigYaml = await resolveBlock( | |
| 407 | + decodePackageIdentifier(rule.uses), | |
| 408 | + rule.with, | |
| 409 | + registry, | |
| 410 | + ); | |
| 411 | + const block = blockConfigYaml.rules?.[0]; | |
| 412 | + return { index, rule: block || null, error: null }; | |
| 413 | + } catch (err) { | |
| 414 | + console.error( | |
| 415 | + `Failed to unroll block ${rule.uses}: ${(err as Error).message}`, | |
| 416 | + ); | |
| 417 | + | |
| 418 | + return { | |
| 419 | + index, | |
| 420 | + rule: null, | |
| 421 | + error: { | |
| 422 | + fatal: false, | |
| 423 | + message: `${(err as Error).message}:\n${rule.uses}`, | |
| 424 | + }, | |
| 425 | + }; | |
| 426 | + } | |
| 427 | + } | |
| 428 | + return { index, rule: null, error: null }; | |
| 429 | + }); | |
| 430 | + | |
| 431 | + const ruleResults = await Promise.all(rulePromises); | |
| 432 | + | |
| 433 | + const rules: (Rule | null)[] = []; | |
| 434 | + const ruleErrors: ConfigValidationError[] = []; | |
| 435 | + | |
| 436 | + for (const result of ruleResults) { | |
| 437 | + if (result.error) { | |
| 438 | + ruleErrors.push(result.error); | |
| 392 | 439 | } |
| 393 | - } catch (err) { | |
| 394 | - errors.push({ | |
| 395 | - fatal: false, | |
| 396 | - message: `${(err as Error).message}:\n${rule.uses}`, | |
| 397 | - }); | |
| 398 | - | |
| 399 | - console.error( | |
| 400 | - `Failed to unroll block ${rule.uses}: ${(err as Error).message}`, | |
| 401 | - ); | |
| 402 | - rules.push(null); | |
| 440 | + rules[result.index] = result.rule; | |
| 403 | 441 | } |
| 404 | - } | |
| 442 | + | |
| 443 | + return { rules, errors: ruleErrors }; | |
| 444 | + })() | |
| 445 | + : Promise.resolve({ rules: undefined, errors: [] }); | |
| 446 | + | |
| 447 | + // Process injected blocks in parallel | |
| 448 | + const injectedBlocksPromise = injectBlocks | |
| 449 | + ? (async () => { | |
| 450 | + const injectedBlockPromises = injectBlocks.map(async (injectBlock) => { | |
| 451 | + try { | |
| 452 | + const blockConfigYaml = await registry.getContent(injectBlock); | |
| 453 | + const parsedBlock = parseMarkdownRuleOrConfigYaml( | |
| 454 | + blockConfigYaml, | |
| 455 | + injectBlock, | |
| 456 | + ); | |
| 457 | + const blockType = getBlockType(parsedBlock); | |
| 458 | + const resolvedBlock = await resolveBlock( | |
| 459 | + injectBlock, | |
| 460 | + undefined, | |
| 461 | + registry, | |
| 462 | + ); | |
| 463 | + | |
| 464 | + return { | |
| 465 | + blockType, | |
| 466 | + resolvedBlock, | |
| 467 | + error: null, | |
| 468 | + }; | |
| 469 | + } catch (err) { | |
| 470 | + let msg = ""; | |
| 471 | + if (injectBlock.uriType === "file") { | |
| 472 | + msg = `${(err as Error).message}.\n> ${injectBlock.filePath}`; | |
| 473 | + } else { | |
| 474 | + msg = `${(err as Error).message}.\n> ${injectBlock.fullSlug}`; | |
| 475 | + } | |
| 476 | + | |
| 477 | + console.error( | |
| 478 | + `Failed to unroll block ${JSON.stringify(injectBlock)}: ${(err as Error).message}`, | |
| 479 | + ); | |
| 480 | + | |
| 481 | + return { | |
| 482 | + blockType: null, | |
| 483 | + resolvedBlock: null, | |
| 484 | + error: { | |
| 485 | + fatal: false, | |
| 486 | + message: msg, | |
| 487 | + }, | |
| 488 | + }; | |
| 489 | + } | |
| 490 | + }); | |
| 491 | + | |
| 492 | + const injectedResults = await Promise.all(injectedBlockPromises); | |
| 493 | + const injectedErrors: ConfigValidationError[] = []; | |
| 494 | + const injectedBlocks: { blockType: string; resolvedBlock: any }[] = []; | |
| 495 | + | |
| 496 | + for (const result of injectedResults) { | |
| 497 | + if (result.error) { | |
| 498 | + injectedErrors.push(result.error); | |
| 499 | + } else if (result.blockType && result.resolvedBlock) { | |
| 500 | + injectedBlocks.push({ | |
| 501 | + blockType: result.blockType, | |
| 502 | + resolvedBlock: result.resolvedBlock, | |
| 503 | + }); | |
| 504 | + } | |
| 505 | + } | |
| 506 | + | |
| 507 | + return { injectedBlocks, errors: injectedErrors }; | |
| 508 | + })() | |
| 509 | + : Promise.resolve({ injectedBlocks: [], errors: [] }); | |
| 510 | + | |
| 511 | + // Wait for all parallel operations to complete | |
| 512 | + const [sectionResults, rulesResult, injectedResult] = await Promise.all([ | |
| 513 | + Promise.all(sectionPromises), | |
| 514 | + rulesPromise, | |
| 515 | + injectedBlocksPromise, | |
| 516 | + ]); | |
| 517 | + | |
| 518 | + // Collect all errors | |
| 519 | + for (const sectionResult of sectionResults) { | |
| 520 | + if (sectionResult.errors) { | |
| 521 | + errors.push(...sectionResult.errors); | |
| 405 | 522 | } |
| 523 | + } | |
| 524 | + errors.push(...rulesResult.errors); | |
| 525 | + errors.push(...injectedResult.errors); | |
| 526 | + | |
| 527 | + // Assign section results | |
| 528 | + for (const sectionResult of sectionResults) { | |
| 529 | + if (sectionResult.blocks) { | |
| 530 | + unrolledAssistant[sectionResult.section] = sectionResult.blocks; | |
| 531 | + } | |
| 532 | + } | |
| 406 | 533 | |
| 407 | - unrolledAssistant.rules = rules; | |
| 534 | + // Assign rules result | |
| 535 | + if (rulesResult.rules) { | |
| 536 | + unrolledAssistant.rules = rulesResult.rules; | |
| 408 | 537 | } |
| 409 | 538 | |
| 410 | 539 | // Add injected blocks |
| 411 | - for (const injectBlock of injectBlocks ?? []) { | |
| 412 | - try { | |
| 413 | - const blockConfigYaml = await registry.getContent(injectBlock); | |
| 414 | - const parsedBlock = parseMarkdownRuleOrConfigYaml( | |
| 415 | - blockConfigYaml, | |
| 416 | - injectBlock, | |
| 417 | - ); | |
| 418 | - const blockType = getBlockType(parsedBlock); | |
| 419 | - const resolvedBlock = await resolveBlock( | |
| 420 | - injectBlock, | |
| 421 | - undefined, | |
| 422 | - registry, | |
| 423 | - ); | |
| 424 | - | |
| 425 | - if (blockType) { | |
| 426 | - if (!unrolledAssistant[blockType]) { | |
| 427 | - unrolledAssistant[blockType] = []; | |
| 428 | - } | |
| 429 | - unrolledAssistant[blockType]?.push( | |
| 430 | - ...(resolvedBlock[blockType] as any), | |
| 431 | - ); | |
| 432 | - } | |
| 433 | - } catch (err) { | |
| 434 | - let msg = ""; | |
| 435 | - if (injectBlock.uriType === "file") { | |
| 436 | - msg = `${(err as Error).message}.\n> ${injectBlock.filePath}`; | |
| 437 | - } else { | |
| 438 | - msg = `${(err as Error).message}.\n> ${injectBlock.fullSlug}`; | |
| 439 | - } | |
| 440 | - errors.push({ | |
| 441 | - fatal: false, | |
| 442 | - message: msg, | |
| 443 | - }); | |
| 444 | - | |
| 445 | - console.error( | |
| 446 | - `Failed to unroll block ${JSON.stringify(injectBlock)}: ${(err as Error).message}`, | |
| 447 | - ); | |
| 540 | + for (const { blockType, resolvedBlock } of injectedResult.injectedBlocks) { | |
| 541 | + const key = blockType as BlockType; | |
| 542 | + if (!unrolledAssistant[key]) { | |
| 543 | + unrolledAssistant[key] = []; | |
| 448 | 544 | } |
| 545 | + unrolledAssistant[key]?.push(...((resolvedBlock[blockType] ?? []) as any)); | |
| 449 | 546 | } |
| 450 | 547 | |
| 451 | 548 | const configResult: ConfigResult<AssistantUnrolled> = { |