(args: Record<string, unknown>)
| 455 | } |
| 456 | |
| 457 | async function handleSnapshotMerge(args: Record<string, unknown>) { |
| 458 | if (args.sourcePath === undefined) { |
| 459 | return snapshotError('sourcePath is required') |
| 460 | } |
| 461 | const parsedArgs = snapshotMergeArgsSchema.safeParse(args) |
| 462 | if (!parsedArgs.success) { |
| 463 | return snapshotError(`Invalid arguments for deepnote_snapshot_merge: ${formatFirstIssue(parsedArgs.error)}`) |
| 464 | } |
| 465 | const { sourcePath, snapshotPath, outputPath } = parsedArgs.data |
| 466 | const skipMismatched = parsedArgs.data.skipMismatched ?? false |
| 467 | |
| 468 | try { |
| 469 | const absoluteSourcePath = path.resolve(sourcePath) |
| 470 | const sourceContent = await fs.readFile(absoluteSourcePath, 'utf-8') |
| 471 | const source = deserializeDeepnoteFile(sourceContent) |
| 472 | |
| 473 | // Load snapshot - either from path or find latest |
| 474 | let snapshot: DeepnoteSnapshot | null = null |
| 475 | if (snapshotPath && snapshotPath !== 'latest') { |
| 476 | const absoluteSnapshotPath = path.resolve(snapshotPath) |
| 477 | snapshot = await loadSnapshotFile(absoluteSnapshotPath) |
| 478 | } else { |
| 479 | const nbId = resolveSnapshotNotebookId(source) |
| 480 | snapshot = await loadLatestSnapshot(absoluteSourcePath, source.project.id, nbId ? { notebookId: nbId } : {}) |
| 481 | |
| 482 | if (!snapshot) { |
| 483 | return { |
| 484 | content: [ |
| 485 | { |
| 486 | type: 'text', |
| 487 | text: JSON.stringify({ |
| 488 | error: 'No snapshot found', |
| 489 | sourcePath: absoluteSourcePath, |
| 490 | hint: 'Provide snapshotPath or run deepnote_snapshot_split first', |
| 491 | }), |
| 492 | }, |
| 493 | ], |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // Merge outputs into source |
| 499 | const merged = mergeSnapshotIntoSource(source, snapshot, { skipMismatched }) |
| 500 | |
| 501 | // Count merged outputs |
| 502 | let outputCount = 0 |
| 503 | for (const notebook of merged.project.notebooks) { |
| 504 | for (const block of notebook.blocks) { |
| 505 | const execBlock = block as { outputs?: unknown[] } |
| 506 | if (execBlock.outputs && execBlock.outputs.length > 0) { |
| 507 | outputCount++ |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | // Save result |
| 513 | const finalPath = outputPath ? path.resolve(outputPath) : absoluteSourcePath |
| 514 | const mergedContent = serializeDeepnoteFile(merged) |
no test coverage detected