Skip to content

Commit fe4813b

Browse files
committed
frontend/llm-context: simplify logic as a first step
1 parent 1f75d9a commit fe4813b

File tree

4 files changed

+27
-71
lines changed

4 files changed

+27
-71
lines changed

src/packages/frontend/jupyter/insert-cell/ai-cell-generator.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,6 @@ export function AIGenerateCodeCell({
454454
onCellTypesChange={setCellTypes}
455455
currentCellId={id}
456456
frameActions={frameActions.current}
457-
mode="insert-position"
458457
/>
459458
</>
460459
);

src/packages/frontend/jupyter/llm/cell-context-selector.tsx

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ export interface LLMCellContextSelectorProps {
1818
cellTypes: "all" | "code";
1919
onCellTypesChange: (types: "all" | "code") => void;
2020

21-
// Current cell ID and frame actions for counting available cells
21+
// Current cell ID and frame actions for enumerating available cells
2222
currentCellId: string;
2323
frameActions: NotebookFrameActions | undefined;
24-
25-
// For ai-cell-generator, the context is relative to where the new cell will be inserted
26-
// For cell-tool, the context is relative to the current cell
27-
mode: "current-cell" | "insert-position";
2824
}
2925

3026
export function LLMCellContextSelector({
@@ -34,68 +30,37 @@ export function LLMCellContextSelector({
3430
onCellTypesChange,
3531
currentCellId,
3632
frameActions,
37-
mode,
3833
}: LLMCellContextSelectorProps) {
3934
const { minValue, maxValue, marks } = useMemo(() => {
4035
const jupyterActionsStore = frameActions?.jupyter_actions.store;
4136
if (!jupyterActionsStore) {
4237
return { minValue: 0, maxValue: 0, marks: { 0: "0" } };
4338
}
4439

45-
let minVal: number, maxVal: number;
46-
47-
if (mode === "insert-position") {
48-
// For insert position, we need to count cells differently
49-
// Count all cells before the insertion point (include current cell and all above it)
50-
let cellsBefore = 0;
51-
let delta = 0; // Start from current cell (which will be "before" after insertion)
52-
while (jupyterActionsStore.get_cell_id(delta, currentCellId)) {
53-
cellsBefore++;
54-
delta--;
55-
}
56-
57-
// Count cells after
58-
let cellsAfter = 0;
59-
delta = 1;
60-
while (jupyterActionsStore.get_cell_id(delta, currentCellId)) {
61-
cellsAfter++;
62-
delta++;
63-
}
64-
65-
minVal = -cellsBefore;
66-
maxVal = cellsAfter;
67-
} else {
68-
// For current-cell mode, count cells above and below as before
69-
let cellsAbove = 0;
70-
let delta = -1;
71-
while (jupyterActionsStore.get_cell_id(delta, currentCellId)) {
72-
cellsAbove++;
73-
delta--;
74-
}
75-
76-
// Count cells below
77-
let cellsBelow = 0;
78-
delta = 1;
79-
while (jupyterActionsStore.get_cell_id(delta, currentCellId)) {
80-
cellsBelow++;
81-
delta++;
82-
}
83-
84-
minVal = -cellsAbove;
85-
maxVal = cellsBelow;
40+
// Count all cells before the insertion point (include current cell and all above it)
41+
let cellsBefore = 0;
42+
while (jupyterActionsStore.get_cell_id(cellsBefore, currentCellId)) {
43+
cellsBefore++;
44+
}
45+
46+
// start with the first cell after the current cell (+1 offset)
47+
let cellsAfter = 0;
48+
while (jupyterActionsStore.get_cell_id(cellsAfter + 1, currentCellId)) {
49+
cellsAfter++;
8650
}
8751

52+
const minVal = -cellsBefore;
53+
const maxVal = cellsAfter;
54+
8855
// Create marks dynamically
89-
const marks: SliderSingleProps["marks"] = {
90-
0: mode === "current-cell" ? "0" : "insert",
91-
};
56+
const marks: SliderSingleProps["marks"] = { 0: "0" };
9257

9358
// Only add boundary marks if they don't conflict with -2/+2
94-
if (minVal < 0) {
95-
marks[minVal] = minVal === -2 ? "-2" : "first";
59+
if (minVal < -3) {
60+
marks[minVal] = "first";
9661
}
97-
if (maxVal > 0) {
98-
marks[maxVal] = maxVal === 2 ? "+2" : "last";
62+
if (maxVal > 3) {
63+
marks[maxVal] = "last";
9964
}
10065

10166
// Add -2 and +2 marks only if they're not at the boundaries
@@ -107,26 +72,19 @@ export function LLMCellContextSelector({
10772
}
10873

10974
return { minValue: minVal, maxValue: maxVal, marks };
110-
}, [currentCellId, frameActions, mode]);
75+
}, [currentCellId, frameActions]);
11176

112-
// Adjust range to be within bounds
77+
// clip range to be within bounds, just to be safe
11378
const adjustedRange: [number, number] = [
11479
Math.max(contextRange[0], minValue),
11580
Math.min(contextRange[1], maxValue),
11681
];
11782

118-
const getDescription = () => {
119-
if (mode === "current-cell") {
120-
return `Selected: ${Math.abs(
121-
adjustedRange[0],
122-
)} cells above + current cell + ${adjustedRange[1]} cells below`;
123-
} else {
124-
// For insert position mode
125-
const beforeCount = Math.abs(adjustedRange[0]);
126-
const afterCount = adjustedRange[1];
127-
return `Selected: ${beforeCount} cells before insertion + ${afterCount} cells after insertion`;
128-
}
129-
};
83+
function getDescription() {
84+
return `Selected: ${Math.abs(
85+
adjustedRange[0],
86+
)} cells above including current cell + ${adjustedRange[1]} cells below`;
87+
}
13088

13189
return (
13290
<>

src/packages/frontend/jupyter/llm/cell-tool.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,6 @@ export function LLMCellTool({
11421142
onCellTypesChange={setCellTypes}
11431143
currentCellId={id}
11441144
frameActions={frameActions.current}
1145-
mode="current-cell"
11461145
/>
11471146
);
11481147
}

src/packages/frontend/project/page/home-page/ai-generate-document.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ function AIGenerateDocument({
578578
if (cancel.current) {
579579
// we abort this
580580
llmStream.removeAllListeners();
581-
// singal "finalization"
581+
// signal "finalization"
582582
processTokens(answer, true);
583583
return;
584584
}

0 commit comments

Comments
 (0)