(args: Record<string, unknown>)
| 607 | } |
| 608 | |
| 609 | async function handleEditBlock(args: Record<string, unknown>) { |
| 610 | const parsedArgs = editBlockArgsSchema.safeParse(args) |
| 611 | if (!parsedArgs.success) { |
| 612 | return writingError(`Invalid args in handleEditBlock: ${formatFirstIssue(parsedArgs.error)}`) |
| 613 | } |
| 614 | const filePath = parsedArgs.data.path |
| 615 | const blockId = parsedArgs.data.blockId |
| 616 | const newContent = parsedArgs.data.content |
| 617 | const newMetadata = parsedArgs.data.metadata |
| 618 | const dryRun = parsedArgs.data.dryRun === true |
| 619 | |
| 620 | const file = await loadDeepnoteFile(filePath) |
| 621 | |
| 622 | // Find the block (supports ID prefix matching) |
| 623 | let targetBlock: DeepnoteBlock | undefined |
| 624 | let targetNotebook: string | undefined |
| 625 | |
| 626 | for (const notebook of file.project.notebooks) { |
| 627 | const block = findBlock(notebook.blocks, blockId) |
| 628 | if (block) { |
| 629 | targetBlock = block |
| 630 | targetNotebook = notebook.name |
| 631 | break |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | if (!targetBlock) { |
| 636 | return { |
| 637 | content: [{ type: 'text', text: `Block not found: ${blockId}` }], |
| 638 | isError: true, |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | const changes: string[] = [] |
| 643 | |
| 644 | if (newContent !== undefined) { |
| 645 | changes.push(`content: "${targetBlock.content?.slice(0, 50)}..." → "${newContent.slice(0, 50)}..."`) |
| 646 | targetBlock.content = newContent |
| 647 | } |
| 648 | |
| 649 | if (newMetadata) { |
| 650 | changes.push(`metadata: merged new keys`) |
| 651 | targetBlock.metadata = { ...targetBlock.metadata, ...newMetadata } |
| 652 | } |
| 653 | |
| 654 | if (dryRun) { |
| 655 | return { |
| 656 | content: [ |
| 657 | { |
| 658 | type: 'text', |
| 659 | text: JSON.stringify( |
| 660 | { |
| 661 | wouldEdit: { |
| 662 | blockId, |
| 663 | notebook: targetNotebook, |
| 664 | changes, |
| 665 | }, |
| 666 | }, |
no test coverage detected