MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / moveSpawnAgentBlock

Function moveSpawnAgentBlock

cli/src/utils/message-block-helpers.ts:463–525  ·  view source on GitHub ↗
(
  blocks: ContentBlock[],
  tempId: string,
  realId: string,
  parentId?: string,
  params?: Record<string, unknown>,
  prompt?: string,
  realAgentType?: string,
)

Source from the content-addressed store, hash-verified

461}
462
463export const moveSpawnAgentBlock = (
464 blocks: ContentBlock[],
465 tempId: string,
466 realId: string,
467 parentId?: string,
468 params?: Record<string, unknown>,
469 prompt?: string,
470 realAgentType?: string,
471): ContentBlock[] => {
472 const updateAgentBlock = (block: ContentBlock): ContentBlock => {
473 if (block.type !== 'agent') {
474 return block
475 }
476 const updatedBlock: ContentBlock = {
477 ...block,
478 agentId: realId,
479 }
480
481 if (params) {
482 updatedBlock.params = params
483 }
484
485 if (prompt && block.initialPrompt === '') {
486 updatedBlock.initialPrompt = prompt
487 }
488
489 if (realAgentType) {
490 updatedBlock.agentType = realAgentType
491 updatedBlock.agentName = realAgentType
492 }
493
494 return updatedBlock
495 }
496
497 // If there's a parentId, we need to move the block under the parent.
498 // First check if the block is already under the correct parent.
499 if (parentId) {
500 const isAlreadyUnderParent = checkBlockIsUnderParent(blocks, tempId, parentId)
501 if (isAlreadyUnderParent) {
502 // Block is already under the correct parent, just update it in place
503 return updateBlocksRecursively(blocks, tempId, updateAgentBlock)
504 }
505
506 // Block needs to be moved under the parent - extract and nest
507 const { remainingBlocks, extractedBlock } = extractBlockById(blocks, tempId)
508 if (extractedBlock && extractedBlock.type === 'agent') {
509 const blockToMove = updateAgentBlock(extractedBlock)
510 const { blocks: nestedBlocks, parentFound } = nestBlockUnderParent(
511 remainingBlocks,
512 parentId,
513 blockToMove,
514 )
515 if (parentFound) {
516 return nestedBlocks
517 }
518 // Parent not found, update in place instead of appending to end
519 return updateBlocksRecursively(blocks, tempId, updateAgentBlock)
520 }

Callers 2

resolveSpawnAgentToRealFunction · 0.90

Calls 5

checkBlockIsUnderParentFunction · 0.85
extractBlockByIdFunction · 0.85
updateAgentBlockFunction · 0.85
nestBlockUnderParentFunction · 0.85
updateBlocksRecursivelyFunction · 0.70

Tested by

no test coverage detected