Skip to content

Commit 2216fa9

Browse files
seefeldbbfollingtoncubic-dev-ai[bot]
authored
fix(llm-dialog): move expensive operations out of reactive path for now (#2244)
* fix(llm-dialog): move expensive operations out of reactive path for now * Fix incorrect `pinnedCells` update logic --------- Co-authored-by: Ben Follington <[email protected]> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1 parent 9b6abec commit 2216fa9

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

packages/runner/src/builtins/llm-dialog.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ function extractRunArguments(input: unknown): Record<string, any> {
537537
* @param toolHandlers - Optional map to populate with handler references for invocation
538538
* @returns Flattened tools object with handler/pattern entries
539539
*/
540-
function flattenTools(
540+
function _flattenTools(
541541
toolsCell: Cell<any>,
542542
_runtime?: Runtime,
543543
): Record<
@@ -628,7 +628,7 @@ function flattenTools(
628628
* (description, inputSchema, internal) and excludes handler which may have
629629
* circular references.
630630
*/
631-
function toolsHaveChanged(
631+
function _toolsHaveChanged(
632632
newTools: Record<string, any>,
633633
oldTools: Record<string, any> | undefined,
634634
): boolean {
@@ -1764,6 +1764,7 @@ export function llmDialog(
17641764
pending,
17651765
internal,
17661766
pinnedCells,
1767+
result,
17671768
requestId,
17681769
abortController.signal,
17691770
);
@@ -1793,6 +1794,9 @@ export function llmDialog(
17931794
// cell as it's the only way to get to requestId, here we are passing it
17941795
// around on the side.
17951796

1797+
// Removing flatted tool support for now, just used in UI and too expensive
1798+
// to recalculate on every context change
1799+
/*
17961800
// Update flattened tools whenever tools change
17971801
const toolsCell = inputs.key("tools");
17981802
// Ensure reactivity: register a read of tools with this tx
@@ -1806,25 +1810,7 @@ export function llmDialog(
18061810
if (hasChanged) {
18071811
result.withTx(tx).key("flattenedTools").set(flattened as any);
18081812
}
1809-
1810-
// Update merged pinnedCells whenever context or internal pinnedCells change
1811-
const contextCells = inputs.key("context").withTx(tx).get() ?? {};
1812-
const toolPinnedCells = pinnedCells.withTx(tx).get() ?? [];
1813-
1814-
// Convert context cells to PinnedCell format
1815-
const contextAsPinnedCells: PinnedCell[] = Object.entries(contextCells).map(
1816-
([name, cell]) => {
1817-
const link = cell.resolveAsCell().getAsNormalizedFullLink();
1818-
const path = createLLMFriendlyLink(link);
1819-
return { name, path };
1820-
},
1821-
);
1822-
1823-
// Merge context cells and tool-pinned cells
1824-
const mergedPinnedCells = [...contextAsPinnedCells, ...toolPinnedCells];
1825-
1826-
// Write to result cell
1827-
result.withTx(tx).key("pinnedCells").set(mergedPinnedCells as any);
1813+
*/
18281814

18291815
if (
18301816
(!result.withTx(tx).key("pending").get() ||
@@ -1847,6 +1833,7 @@ function startRequest(
18471833
pending: Cell<boolean>,
18481834
internal: Cell<Schema<typeof internalSchema>>,
18491835
pinnedCells: Cell<PinnedCell[]>,
1836+
result: Cell<Schema<typeof resultSchema>>,
18501837
requestId: string,
18511838
abortSignal: AbortSignal,
18521839
) {
@@ -1857,7 +1844,27 @@ function startRequest(
18571844
Record<string, Schema<typeof LLMToolSchema>>
18581845
>;
18591846

1860-
// No need to flatten here; UI handles flattened tools reactively
1847+
// Update merged pinnedCells in case context or internal pinnedCells changed
1848+
const contextCells = inputs.key("context").withTx(tx).get() ?? {};
1849+
const toolPinnedCells = pinnedCells.withTx(tx).get() ?? [];
1850+
1851+
const contextAsPinnedCells: PinnedCell[] = Object.entries(contextCells)
1852+
// Convert context cells to PinnedCell format
1853+
.map(
1854+
([name, cell]) => {
1855+
const link = cell.resolveAsCell().getAsNormalizedFullLink();
1856+
const path = createLLMFriendlyLink(link);
1857+
return { name, path };
1858+
},
1859+
)
1860+
// Remove pinned cells that are already in the context
1861+
.filter(({ path }) => !toolPinnedCells.some((cell) => cell.path === path));
1862+
1863+
// Merge context cells and tool-pinned cells
1864+
const mergedPinnedCells = [...contextAsPinnedCells, ...toolPinnedCells];
1865+
1866+
// Write to result cell
1867+
result.withTx(tx).key("pinnedCells").set(mergedPinnedCells as any);
18611868

18621869
const toolCatalog = buildToolCatalog(toolsCell);
18631870

@@ -2074,6 +2081,7 @@ Some operations (especially \`invoke()\` with patterns) create "Pages" - running
20742081
pending,
20752082
internal,
20762083
pinnedCells,
2084+
result,
20772085
requestId,
20782086
abortSignal,
20792087
);

0 commit comments

Comments
 (0)