Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5fa638d
WIP
markmiro Sep 22, 2025
f04566e
WIP first pass dropdown menu
markmiro Sep 22, 2025
f1f6dab
Move hide AI cells button
markmiro Sep 22, 2025
35e3ecc
Add toasts for stopping cells
markmiro Sep 22, 2025
1395edc
Implement hide AI cells
markmiro Sep 22, 2025
67fceb2
Show badge if cells running or AI cells are hidden
markmiro Sep 22, 2025
7bc676c
Use standard function for generating a queue id
markmiro Sep 22, 2025
1d2a61b
Fix stop all cells
markmiro Sep 22, 2025
db46fd8
Do filtering on sqlite side
markmiro Sep 23, 2025
d1c6ccd
Show message if user adds AI cell if AI cells are hidden
markmiro Sep 23, 2025
6620106
Add warning if hiding AI cells
markmiro Sep 23, 2025
2233175
Use a single event for running all cells
markmiro Sep 23, 2025
5bf1e59
Use single event for clearing all outputs and stopping execution
markmiro Sep 23, 2025
e9b5397
Update queue id generation to resist collisions better
markmiro Sep 23, 2025
823953e
Rename stop to cancel
markmiro Sep 23, 2025
43ae3e2
Cancel all without arguments
markmiro Sep 23, 2025
2b05c15
Use past tense
markmiro Sep 23, 2025
4168b54
Clear all with almost no arguments
markmiro Sep 23, 2025
45878c1
Update toast
markmiro Sep 23, 2025
afefdd5
Don't return ops
markmiro Sep 23, 2025
2e1954e
Merge branch 'main' into nb-controls-2
markmiro Sep 30, 2025
2c9edc3
Fix getting cells
markmiro Sep 29, 2025
868b90a
Remove unused
markmiro Sep 30, 2025
f703042
Move runnableCellsWithIndices$
markmiro Sep 30, 2025
c574980
Remove hide AI cells NB control
markmiro Sep 30, 2025
ad32bf8
Change menu titles
markmiro Sep 30, 2025
56abeab
Fix type error
markmiro Sep 30, 2025
ed1d9cb
Show live running status
markmiro Sep 30, 2025
0f534b0
Remove badge
markmiro Sep 30, 2025
600721e
merge: main to nb-controls-2
markmiro Oct 28, 2025
ad088e1
Put bulk nb controls behind a feature flag
markmiro Oct 28, 2025
1cb09b7
Merge branch 'main' into nb-controls-2
markmiro Oct 28, 2025
c705d1b
Remove unused
markmiro Oct 28, 2025
68b86cc
Move new queries into one place
markmiro Oct 28, 2025
c5cc287
Add extra checks
markmiro Oct 28, 2025
c43d895
Undo schema change
markmiro Oct 28, 2025
7808a31
Remove unimportant changes
markmiro Oct 28, 2025
fa0f0cf
Merge branch 'main' into nb-controls-2
markmiro Oct 28, 2025
ed6e6d3
Merge branch 'main' into nb-controls-2
markmiro Oct 28, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/components/notebook/cell/ExecutableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { MaybeCellOutputs } from "@/components/outputs/MaybeCellOutputs.js";
import { useToolApprovals } from "@/hooks/useToolApprovals.js";
import { AiToolApprovalOutput } from "../../outputs/shared-with-iframe/AiToolApprovalOutput.js";
import { cn } from "@/lib/utils.js";
import { generateQueueId } from "@/util/queue-id.js";
import { useTrpc } from "@/components/TrpcProvider.js";
import { cycleCellType } from "@/util/cycle-cell-type.js";
import { useFeatureFlag } from "@/contexts/FeatureFlagContext.js";
Expand Down Expand Up @@ -246,15 +247,12 @@ export const ExecutableCell: React.FC<ExecutableCellProps> = ({
);

// Generate unique queue ID
const queueId = `exec-${Date.now()}-${Math.random()
.toString(36)
.slice(2)}`;
const executionCount = (cell.executionCount || 0) + 1;

// Add to execution queue - runtimes will pick this up
store.commit(
events.executionRequested({
queueId,
queueId: generateQueueId(),
cellId: cell.id,
executionCount,
requestedBy: userId,
Expand Down
200 changes: 199 additions & 1 deletion src/components/notebooks/notebook/NotebookControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,157 @@ import { toast } from "sonner";
import { useCreateNotebookAndNavigate } from "../dashboard/helpers";
import type { NotebookProcessed } from "../types";

import { useAuthenticatedUser } from "@/auth/index.js";
import { useDebug } from "@/components/debug/debug-mode";
import { Spinner } from "@/components/ui/Spinner";
import { useFeatureFlag } from "@/contexts/FeatureFlagContext";
import { useRuntimeHealth } from "@/hooks/useRuntimeHealth";
import { runnableCellsWithIndices$, runningCells$ } from "@/queries";
import { generateQueueId } from "@/util/queue-id";
import { useQuery, useStore } from "@livestore/react";
import { CellData, events, queries } from "@runtimed/schema";
import { Eraser, Play, Square, Undo2 } from "lucide-react";
import { useCallback } from "react";

export function NotebookControls({
notebook,
}: {
notebook: NotebookProcessed;
}) {
const allowBulkNotebookControls = useFeatureFlag("bulk-notebook-controls");
const { store } = useStore();
const userId = useAuthenticatedUser();
const debug = useDebug();

const cellQueue = useQuery(runningCells$);

const handleCancelAll = useCallback(() => {
if (!allowBulkNotebookControls) {
toast.info("Bulk notebook controls are not enabled");
return;
}
if (cellQueue.length === 0) {
toast.info("No cells to stop");
return;
}
store.commit(events.allExecutionsCancelled());
toast.info("Cancelled all executions");
}, [store, cellQueue, allowBulkNotebookControls]);

const handleClearAllOutputs = useCallback(() => {
if (!allowBulkNotebookControls) {
toast.info("Bulk notebook controls are not enabled");
return;
}
store.commit(events.allOutputsCleared({ clearedBy: userId }));
}, [store, userId, allowBulkNotebookControls]);

return (
<div className="flex items-center gap-1">
<div className="flex items-center gap-2">
{allowBulkNotebookControls && (
<ActiveBulkNotebookActions
cellQueue={cellQueue}
onCancelAll={handleCancelAll}
/>
)}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="sm" className="relative">
<MoreHorizontal className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{allowBulkNotebookControls && (
<BulkNotebookActions
cellQueue={cellQueue}
onCancelAll={handleCancelAll}
onClearAllOutputs={handleClearAllOutputs}
/>
)}
<CreateNotebookAction />
<DuplicateAction notebook={notebook} />
<DropdownMenuSeparator />
{debug.enabled && <DeleteAllCellsAction />}
<DeleteAction notebook={notebook} />
</DropdownMenuContent>
</DropdownMenu>
</div>
);
}

function ActiveBulkNotebookActions({
cellQueue,
onCancelAll,
}: {
cellQueue: readonly CellData[];
onCancelAll: () => void;
}) {
if (cellQueue.length === 0) {
return null;
}

return (
<>
<span className="flex items-center gap-1">
<Spinner size="md" />
<span className="text-muted-foreground text-sm">
Running {cellQueue.length} cells
</span>
</span>
<Button variant="outline" size="sm" onClick={onCancelAll}>
<Square />
Stop All
</Button>
</>
);
}

function BulkNotebookActions({
cellQueue,
onCancelAll,
onClearAllOutputs,
}: {
cellQueue: readonly CellData[];
onCancelAll: () => void;
onClearAllOutputs: () => void;
}) {
const { runAllCells } = useRunAllCells();
const { restartAndRunAllCells } = useRestartAndRunAllCells();

return (
<>
{cellQueue.length > 0 ? (
<DropdownMenuItem onClick={onCancelAll}>
<Square />
Stop All
</DropdownMenuItem>
) : (
<>
<DropdownMenuItem onClick={runAllCells}>
<Play />
Run All Code Cells
</DropdownMenuItem>
<DropdownMenuItem onClick={restartAndRunAllCells}>
<span className="relative">
<Play className="h-4 w-4" />
<Undo2
className="absolute bottom-0 left-0 size-3 -translate-x-[3px] translate-y-[3px] rounded-full bg-white p-[1px] text-gray-700"
strokeWidth={3}
/>
</span>
Restart and Run All Code Cells
</DropdownMenuItem>
</>
)}
<DropdownMenuItem onClick={onClearAllOutputs}>
<Eraser />
Clear All Outputs
</DropdownMenuItem>
<DropdownMenuSeparator />
</>
);
}

function CreateNotebookAction() {
const createNotebookAndNavigate = useCreateNotebookAndNavigate();

Expand Down Expand Up @@ -126,3 +253,74 @@ function DeleteAction({ notebook }: { notebook: NotebookProcessed }) {
</DropdownMenuItem>
);
}

function DeleteAllCellsAction() {
const { deleteAllCells } = useDeleteAllCells();
const { confirm } = useConfirm();

const handleDeleteAllCells = async () => {
confirm({
title: "Delete All Cells",
description: "Are you sure you want to delete all cells?",
onConfirm: deleteAllCells,
actionButtonText: "Delete All Cells",
});
};
return (
<DropdownMenuItem variant="destructive" onSelect={handleDeleteAllCells}>
<Trash2 />
DEBUG: Delete All Cells
</DropdownMenuItem>
);
}

function useDeleteAllCells() {
const { store } = useStore();
const cells = useQuery(queries.cellsWithIndices$);
const userId = useAuthenticatedUser();

const deleteAllCells = useCallback(() => {
cells.forEach((cell) => {
store.commit(events.cellDeleted({ id: cell.id, actorId: userId }));
});
}, [cells, store, userId]);

return { deleteAllCells };
}

function useRunAllCells() {
const { store } = useStore();
const cells = useQuery(runnableCellsWithIndices$);
const userId = useAuthenticatedUser();

const runAllCells = useCallback(() => {
store.commit(
events.multipleExecutionRequested({
requestedBy: userId,
cellsInfo: [
...cells.map((cell) => ({
id: cell.id,
executionCount: (cell.executionCount || 0) + 1,
queueId: generateQueueId(),
})),
],
})
);
}, [store, userId, cells]);

return { runAllCells };
}

function useRestartAndRunAllCells() {
const { hasActiveRuntime } = useRuntimeHealth();

const restartAndRunAllCells = useCallback(() => {
if (hasActiveRuntime) {
toast.info("Restart your runtime manually and run all cells");
} else {
toast.error("No active runtime found");
}
}, [hasActiveRuntime]);

return { restartAndRunAllCells };
}
4 changes: 4 additions & 0 deletions src/contexts/FeatureFlagContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface FeatureFlags {
"test-flag": boolean;
"ipynb-export": boolean;
"file-upload": boolean;
/** Whether to enable the notebook controls */
"bulk-notebook-controls": boolean;
/** Show AI capabilities in the AI cell dropdown. We'd enable this by default if we support vision or allow choosing models that don't have tool support. */
"show-ai-capabilities": boolean;
"user-saved-prompt": boolean;
Expand All @@ -16,7 +18,9 @@ const DEFAULT_FLAGS: FeatureFlags = {
"test-flag": false,
"ipynb-export": false,
"file-upload": false,
"bulk-notebook-controls": false,
"show-ai-capabilities": false,

"user-saved-prompt": false,
} as const;

Expand Down
16 changes: 16 additions & 0 deletions src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ export const availableFiles$ = queryDb(
tables.files.select().where({ deletedAt: null }),
{ label: "files.availableFiles" }
);

export const runnableCellsWithIndices$ = queryDb(
tables.cells
.select("id", "fractionalIndex", "cellType", "executionCount")
.where({ cellType: { op: "IN", value: ["code", "sql"] } })
.orderBy("fractionalIndex", "asc"),
{ label: "cells.withIndices.runnable" }
);

export const runningCells$ = queryDb(
tables.cells
.select()
.where({ executionState: { op: "IN", value: ["running", "queued"] } })
.orderBy("fractionalIndex", "asc"),
{ label: "cells.runningCells" }
);
9 changes: 9 additions & 0 deletions src/util/queue-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Generates a unique queue ID for an execution request
*/
export function generateQueueId() {
// Date should prevent most of the collisions, but not as reliable when making bulk requests
// 6 char length + alphabet of 36 chars = 6K IDs to have 1% chance of collision
// See: https://zelark.github.io/nano-id-cc/
return `exec-${Date.now()}-${Math.random().toString(36).slice(6)}`;
}