(args: Record<string, unknown>)
| 771 | } |
| 772 | |
| 773 | async function handleReorderBlocks(args: Record<string, unknown>) { |
| 774 | const parsedArgs = reorderBlocksArgsSchema.safeParse(args) |
| 775 | if (!parsedArgs.success) { |
| 776 | return writingError(`Invalid args in handleReorderBlocks: ${formatFirstIssue(parsedArgs.error)}`) |
| 777 | } |
| 778 | const filePath = parsedArgs.data.path |
| 779 | const notebookFilter = parsedArgs.data.notebook |
| 780 | const blockIds = parsedArgs.data.blockIds |
| 781 | const dryRun = parsedArgs.data.dryRun === true |
| 782 | |
| 783 | const file = await loadDeepnoteFile(filePath) |
| 784 | if (file.project.notebooks.length === 0) { |
| 785 | return { |
| 786 | content: [{ type: 'text', text: `No notebooks in project "${file.project.name}"` }], |
| 787 | isError: true, |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | // Find the target notebook |
| 792 | let notebook = file.project.notebooks[0] |
| 793 | if (notebookFilter) { |
| 794 | const found = file.project.notebooks.find(n => n.name === notebookFilter || n.id === notebookFilter) |
| 795 | if (!found) { |
| 796 | return { |
| 797 | content: [{ type: 'text', text: `Notebook not found: ${notebookFilter}` }], |
| 798 | isError: true, |
| 799 | } |
| 800 | } |
| 801 | notebook = found |
| 802 | } |
| 803 | |
| 804 | // Resolve all block IDs (support prefix matching) |
| 805 | const resolvedIds: string[] = [] |
| 806 | const resolvedIdSet = new Set<string>() |
| 807 | for (const id of blockIds) { |
| 808 | const fullId = resolveBlockId(notebook.blocks, id) |
| 809 | if (!fullId) { |
| 810 | return { |
| 811 | content: [{ type: 'text', text: `Block not found: ${id}` }], |
| 812 | isError: true, |
| 813 | } |
| 814 | } |
| 815 | if (resolvedIdSet.has(fullId)) { |
| 816 | return { |
| 817 | content: [{ type: 'text', text: `Duplicate block ID in blockIds: ${id}` }], |
| 818 | isError: true, |
| 819 | } |
| 820 | } |
| 821 | resolvedIds.push(fullId) |
| 822 | resolvedIdSet.add(fullId) |
| 823 | } |
| 824 | |
| 825 | // Build a map of blocks by ID |
| 826 | const blockMap = new Map(notebook.blocks.map(b => [b.id, b])) |
| 827 | |
| 828 | // Reorder blocks |
| 829 | const reorderedBlocks = resolvedIds |
| 830 | .map(id => blockMap.get(id)) |
no test coverage detected