Skip to content

Commit 9b3e732

Browse files
committed
fix orphaned blocks issue
1 parent 0367cc2 commit 9b3e732

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

frontend/app/store/services.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import * as WOS from "./wos";
77

88
// blockservice.BlockService (block)
99
class BlockServiceType {
10+
// queue a layout action to cleanup orphaned blocks in the tab
11+
// @returns object updates
12+
CleanupOrphanedBlocks(tabId: string): Promise<void> {
13+
return WOS.callBackendService("block", "CleanupOrphanedBlocks", Array.from(arguments))
14+
}
1015
GetControllerStatus(arg2: string): Promise<BlockControllerRuntimeStatus> {
1116
return WOS.callBackendService("block", "GetControllerStatus", Array.from(arguments))
1217
}

frontend/app/view/codeeditor/diffviewer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import { useOverrideConfigAtom } from "@/app/store/global";
55
import { DiffEditor } from "@monaco-editor/react";
66
import type * as MonacoTypes from "monaco-editor/esm/vs/editor/editor.api";
7-
import React, { useMemo } from "react";
7+
import { useMemo } from "react";
88

99
import { boundNumber } from "@/util/util";
1010

@@ -66,4 +66,4 @@ export function DiffViewer({ blockId, original, modified, language, fileName }:
6666
</div>
6767
</div>
6868
);
69-
}
69+
}

frontend/layout/lib/layoutModel.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import { FocusManager } from "@/app/store/focusManager";
55
import { getSettingsKeyAtom } from "@/app/store/global";
6+
import { BlockService } from "@/app/store/services";
67
import { atomWithThrottle, boundNumber, fireAndForget } from "@/util/util";
78
import { Atom, atom, Getter, PrimitiveAtom, Setter } from "jotai";
89
import { splitAtom } from "jotai/utils";
@@ -406,6 +407,26 @@ export class LayoutModel {
406407
this.persistToBackend();
407408
}
408409

410+
private async cleanupOrphanedBlocks() {
411+
const tab = this.getter(this.tabAtom);
412+
const layoutBlockIds = new Set<string>();
413+
414+
walkNodes(this.treeState.rootNode, (node) => {
415+
if (node.data?.blockId) {
416+
layoutBlockIds.add(node.data.blockId);
417+
}
418+
});
419+
420+
for (const blockId of tab.blockids || []) {
421+
if (!layoutBlockIds.has(blockId)) {
422+
console.log("Cleaning up orphaned block:", blockId);
423+
if (this.onNodeDelete) {
424+
await this.onNodeDelete({ blockId });
425+
}
426+
}
427+
}
428+
}
429+
409430
private async handleBackendAction(action: LayoutActionData) {
410431
switch (action.actiontype) {
411432
case LayoutTreeActionType.InsertNode: {
@@ -537,6 +558,10 @@ export class LayoutModel {
537558
this.treeReducer(splitAction, false);
538559
break;
539560
}
561+
case "cleanuporphaned": {
562+
await this.cleanupOrphanedBlocks();
563+
break;
564+
}
540565
default:
541566
console.warn("unsupported layout action", action);
542567
break;
@@ -574,6 +599,8 @@ export class LayoutModel {
574599
if (contents.gapSizePx !== undefined) {
575600
this.setter(this.gapSizePx, contents.gapSizePx);
576601
}
602+
const tab = this.getter(this.tabAtom);
603+
fireAndForget(() => BlockService.CleanupOrphanedBlocks(tab.oid));
577604
}
578605

579606
/**

pkg/aiusechat/tools.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ func MakeBlockShortDesc(block *waveobj.Block) string {
120120
return "placeholder widget used to launch other widgets"
121121
case "tsunami":
122122
return handleTsunamiBlockDesc(block)
123+
case "aifilediff":
124+
return "" // AI doesn't need to see these
123125
default:
124126
return fmt.Sprintf("unknown widget with type %q", viewType)
125127
}

pkg/service/blockservice/blockservice.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99
"fmt"
1010
"time"
1111

12+
"github.com/google/uuid"
1213
"github.com/wavetermdev/waveterm/pkg/blockcontroller"
1314
"github.com/wavetermdev/waveterm/pkg/filestore"
1415
"github.com/wavetermdev/waveterm/pkg/tsgen/tsgenmeta"
1516
"github.com/wavetermdev/waveterm/pkg/waveobj"
17+
"github.com/wavetermdev/waveterm/pkg/wcore"
1618
"github.com/wavetermdev/waveterm/pkg/wshrpc"
1719
"github.com/wavetermdev/waveterm/pkg/wstore"
1820
)
@@ -87,3 +89,23 @@ func (bs *BlockService) SaveWaveAiData(ctx context.Context, blockId string, hist
8789
}
8890
return nil
8991
}
92+
93+
func (*BlockService) CleanupOrphanedBlocks_Meta() tsgenmeta.MethodMeta {
94+
return tsgenmeta.MethodMeta{
95+
Desc: "queue a layout action to cleanup orphaned blocks in the tab",
96+
ArgNames: []string{"ctx", "tabId"},
97+
}
98+
}
99+
100+
func (bs *BlockService) CleanupOrphanedBlocks(ctx context.Context, tabId string) (waveobj.UpdatesRtnType, error) {
101+
ctx = waveobj.ContextWithUpdates(ctx)
102+
layoutAction := waveobj.LayoutActionData{
103+
ActionType: wcore.LayoutActionDataType_CleanupOrphaned,
104+
ActionId: uuid.NewString(),
105+
}
106+
err := wcore.QueueLayoutActionForTab(ctx, tabId, layoutAction)
107+
if err != nil {
108+
return nil, fmt.Errorf("error queuing cleanup layout action: %w", err)
109+
}
110+
return waveobj.ContextGetUpdatesRtn(ctx), nil
111+
}

pkg/wcore/layout.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
LayoutActionDataType_Replace = "replace"
2323
LayoutActionDataType_SplitHorizontal = "splithorizontal"
2424
LayoutActionDataType_SplitVertical = "splitvertical"
25+
LayoutActionDataType_CleanupOrphaned = "cleanuporphaned"
2526
)
2627

2728
type PortableLayout []struct {

0 commit comments

Comments
 (0)