MCPcopy Index your code
hub / github.com/deepnote/deepnote / handleSnapshotMerge

Function handleSnapshotMerge

packages/mcp/src/tools/snapshots.ts:457–540  ·  view source on GitHub ↗
(args: Record<string, unknown>)

Source from the content-addressed store, hash-verified

455}
456
457async 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)

Callers 1

handleSnapshotToolFunction · 0.85

Calls 8

deserializeDeepnoteFileFunction · 0.90
loadSnapshotFileFunction · 0.90
loadLatestSnapshotFunction · 0.90
mergeSnapshotIntoSourceFunction · 0.90
serializeDeepnoteFileFunction · 0.90
snapshotErrorFunction · 0.85
formatFirstIssueFunction · 0.70

Tested by

no test coverage detected