Skip to content

Commit 93ebf3f

Browse files
committed
frontend/juypter/ai-cell-generator: tricky off-by-one error needs to be fixed ...
1 parent d6393eb commit 93ebf3f

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,20 +196,26 @@ export function AIGenerateCodeCell({
196196
}, [preview, open]);
197197

198198
function getContextContents(): CellContextContent {
199-
const prevCount = -contextRange[0]; // contextRange[0] is negative, so -(-2) = 2
200-
const nextCount = contextRange[1]; // contextRange[1] is positive for cells after
199+
const [rangeStart, rangeEnd] = contextRange;
201200

202-
if (prevCount === 0 && nextCount === 0) return {};
201+
// For insertion mode:
202+
// - rangeStart to rangeEnd defines which cells relative to insertion point to include
203+
// - Current cell id represents position 0 (insertion point)
204+
// - [-2, 0] means include cells from id-1 to id (2 cells before insertion)
205+
// - [0, 2] means include cells from id+1 to id+2 (2 cells after insertion)
206+
207+
const aboveCount = rangeStart < 0 ? Math.abs(rangeStart) : 0;
208+
const belowCount = rangeEnd > 0 ? rangeEnd : 0;
203209

204210
return getNonemptyCellContents({
205211
actions: frameActions.current,
206212
id,
207213
direction: "around",
208-
cellCount: "all", // Use "all" for around direction
214+
cellCount: "all",
209215
cellTypes,
210216
lang,
211-
aboveCount: prevCount,
212-
belowCount: nextCount,
217+
aboveCount,
218+
belowCount,
213219
});
214220
}
215221

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,49 @@ export function LLMCellContextSelector({
4242
return { minValue: 0, maxValue: 0, marks: { 0: "0" } };
4343
}
4444

45-
// Count cells above
46-
let cellsAbove = 0;
47-
let delta = -1;
48-
while (jupyterActionsStore.get_cell_id(delta, currentCellId)) {
49-
cellsAbove++;
50-
delta--;
51-
}
52-
53-
// Count cells below
54-
let cellsBelow = 0;
55-
delta = 1;
56-
while (jupyterActionsStore.get_cell_id(delta, currentCellId)) {
57-
cellsBelow++;
58-
delta++;
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;
5986
}
6087

61-
const minVal = -cellsAbove;
62-
const maxVal = cellsBelow;
63-
6488
// Create marks dynamically
6589
const marks: SliderSingleProps["marks"] = {
6690
0: mode === "current-cell" ? "0" : "insert",

src/packages/frontend/jupyter/util/cell-content.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function getNonemptyCellContents({
7070
lang,
7171
direction: "below",
7272
count: belowCount,
73+
includeCurrentCell: false,
7374
});
7475
if (belowContent) result.after = belowContent;
7576
}
@@ -82,10 +83,10 @@ export function getNonemptyCellContents({
8283
typeof cellCount === "number"
8384
? cellCount
8485
: cellCount === "all above" ||
85-
cellCount === "all below" ||
86-
cellCount === "all"
87-
? 100
88-
: 0;
86+
cellCount === "all below" ||
87+
cellCount === "all"
88+
? 100
89+
: 0;
8990

9091
const content = getDirectionalContent({
9192
actions,

0 commit comments

Comments
 (0)