From 512797860260633df3d6838fc93242bf05e0c2d0 Mon Sep 17 00:00:00 2001 From: Sylvia Crowe Date: Mon, 13 Jan 2025 10:37:35 -0800 Subject: [PATCH 1/4] feat: add block id to connection context --- pkg/genconn/genconn.go | 26 ++++++++++++++++++++++++++ pkg/wshrpc/wshserver/wshserver.go | 5 +++++ 2 files changed, 31 insertions(+) diff --git a/pkg/genconn/genconn.go b/pkg/genconn/genconn.go index d81ed616e6..bac3d66c1a 100644 --- a/pkg/genconn/genconn.go +++ b/pkg/genconn/genconn.go @@ -15,6 +15,32 @@ import ( "github.com/wavetermdev/waveterm/pkg/util/syncbuf" ) +type connContextKeyType struct{} + +var connContextKey connContextKeyType + +type connData struct { + BlockId string +} + +func ContextWithConnData(ctx context.Context, blockId string) context.Context { + if blockId == "" { + return ctx + } + return context.WithValue(ctx, connContextKey, &connData{BlockId: blockId}) +} + +func GetConnData(ctx context.Context) *connData { + if ctx == nil { + return nil + } + dataPtr := ctx.Value(connContextKey) + if dataPtr == nil { + return nil + } + return dataPtr.(*connData) +} + type CommandSpec struct { Cmd string Env map[string]string diff --git a/pkg/wshrpc/wshserver/wshserver.go b/pkg/wshrpc/wshserver/wshserver.go index cb7d9ff646..d62f591968 100644 --- a/pkg/wshrpc/wshserver/wshserver.go +++ b/pkg/wshrpc/wshserver/wshserver.go @@ -21,6 +21,7 @@ import ( "github.com/wavetermdev/waveterm/pkg/blockcontroller" "github.com/wavetermdev/waveterm/pkg/blocklogger" "github.com/wavetermdev/waveterm/pkg/filestore" + "github.com/wavetermdev/waveterm/pkg/genconn" "github.com/wavetermdev/waveterm/pkg/panichandler" "github.com/wavetermdev/waveterm/pkg/remote" "github.com/wavetermdev/waveterm/pkg/remote/conncontroller" @@ -238,6 +239,7 @@ func (ws *WshServer) ControllerStopCommand(ctx context.Context, blockId string) } func (ws *WshServer) ControllerResyncCommand(ctx context.Context, data wshrpc.CommandControllerResyncData) error { + ctx = genconn.ContextWithConnData(ctx, data.BlockId) ctx = termCtxWithLogBlockId(ctx, data.BlockId) return blockcontroller.ResyncController(ctx, data.TabId, data.BlockId, data.RtOpts, data.ForceRestart) } @@ -627,6 +629,7 @@ func termCtxWithLogBlockId(ctx context.Context, logBlockId string) context.Conte } func (ws *WshServer) ConnEnsureCommand(ctx context.Context, data wshrpc.ConnExtData) error { + ctx = genconn.ContextWithConnData(ctx, data.LogBlockId) ctx = termCtxWithLogBlockId(ctx, data.LogBlockId) if strings.HasPrefix(data.ConnName, "wsl://") { distroName := strings.TrimPrefix(data.ConnName, "wsl://") @@ -656,6 +659,7 @@ func (ws *WshServer) ConnDisconnectCommand(ctx context.Context, connName string) } func (ws *WshServer) ConnConnectCommand(ctx context.Context, connRequest wshrpc.ConnRequest) error { + ctx = genconn.ContextWithConnData(ctx, connRequest.LogBlockId) ctx = termCtxWithLogBlockId(ctx, connRequest.LogBlockId) connName := connRequest.Host if strings.HasPrefix(connName, "wsl://") { @@ -678,6 +682,7 @@ func (ws *WshServer) ConnConnectCommand(ctx context.Context, connRequest wshrpc. } func (ws *WshServer) ConnReinstallWshCommand(ctx context.Context, data wshrpc.ConnExtData) error { + ctx = genconn.ContextWithConnData(ctx, data.LogBlockId) ctx = termCtxWithLogBlockId(ctx, data.LogBlockId) connName := data.ConnName if strings.HasPrefix(connName, "wsl://") { From 550013723eccadff4ec26ef1b585b111b6f064a7 Mon Sep 17 00:00:00 2001 From: Sylvia Crowe Date: Tue, 14 Jan 2025 13:45:34 -0800 Subject: [PATCH 2/4] fix: remove old debug code --- frontend/app/modals/userinputmodal.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/frontend/app/modals/userinputmodal.tsx b/frontend/app/modals/userinputmodal.tsx index f869c3fd81..9fbaec8005 100644 --- a/frontend/app/modals/userinputmodal.tsx +++ b/frontend/app/modals/userinputmodal.tsx @@ -37,7 +37,6 @@ const UserInputModal = (userInputRequest: UserInputRequest) => { ); modalsModel.popModal(); }, [responseText, userInputRequest]); - console.log("bar"); const handleSendConfirm = useCallback( (response: boolean) => { @@ -64,7 +63,6 @@ const UserInputModal = (userInputRequest: UserInputRequest) => { break; } }, [handleSendConfirm, handleSendText, userInputRequest.responsetype]); - console.log("baz"); const handleKeyDown = useCallback( (waveEvent: WaveKeyboardEvent): boolean => { @@ -86,7 +84,6 @@ const UserInputModal = (userInputRequest: UserInputRequest) => { } return {userInputRequest.querytext}; }, [userInputRequest.markdown, userInputRequest.querytext]); - console.log("foobarbaz"); const inputBox = useMemo(() => { if (userInputRequest.responsetype === "confirm") { @@ -104,7 +101,6 @@ const UserInputModal = (userInputRequest: UserInputRequest) => { /> ); }, [userInputRequest.responsetype, userInputRequest.publictext, responseText, handleKeyDown, setResponseText]); - console.log("mem1"); const optionalCheckbox = useMemo(() => { if (userInputRequest.checkboxmsg == "") { @@ -124,7 +120,6 @@ const UserInputModal = (userInputRequest: UserInputRequest) => { ); }, []); - console.log("mem2"); useEffect(() => { let timeout: ReturnType; @@ -139,7 +134,6 @@ const UserInputModal = (userInputRequest: UserInputRequest) => { } return () => clearTimeout(timeout); }, [countdown]); - console.log("count"); const handleNegativeResponse = useCallback(() => { switch (userInputRequest.responsetype) { @@ -151,7 +145,6 @@ const UserInputModal = (userInputRequest: UserInputRequest) => { break; } }, [userInputRequest.responsetype, handleSendErrResponse, handleSendConfirm]); - console.log("before end"); return ( Date: Tue, 14 Jan 2025 14:08:01 -0800 Subject: [PATCH 3/4] fix: limit wps message to one window This determines the correct block id and sends the modal only to the window that contains the block that made the initial request. --- frontend/app/store/global.ts | 5 +++-- frontend/wave.ts | 2 +- pkg/userinput/userinput.go | 28 +++++++++++++++++++++++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/frontend/app/store/global.ts b/frontend/app/store/global.ts index 70492613bf..e9bf55de1f 100644 --- a/frontend/app/store/global.ts +++ b/frontend/app/store/global.ts @@ -168,7 +168,7 @@ function initGlobalAtoms(initOpts: GlobalInitOptions) { }; } -function initGlobalWaveEventSubs() { +function initGlobalWaveEventSubs(initOpts: WaveInitOpts) { waveEventSubscribe( { eventType: "waveobj:update", @@ -189,10 +189,11 @@ function initGlobalWaveEventSubs() { { eventType: "userinput", handler: (event) => { - // console.log("userinput event handler", event); + console.log("userinput event handler", event); const data: UserInputRequest = event.data; modalsModel.pushModal("UserInputModal", { ...data }); }, + scope: initOpts.windowId, }, { eventType: "blockfile", diff --git a/frontend/wave.ts b/frontend/wave.ts index 463412b760..7123f2868b 100644 --- a/frontend/wave.ts +++ b/frontend/wave.ts @@ -163,7 +163,7 @@ async function initWave(initOpts: WaveInitOpts) { (window as any).globalWS = globalWS; (window as any).TabRpcClient = TabRpcClient; await loadConnStatus(); - initGlobalWaveEventSubs(); + initGlobalWaveEventSubs(initOpts); subscribeToConnEvents(); // ensures client/window/workspace are loaded into the cache before rendering diff --git a/pkg/userinput/userinput.go b/pkg/userinput/userinput.go index 6fc98c2b8f..a2f6697726 100644 --- a/pkg/userinput/userinput.go +++ b/pkg/userinput/userinput.go @@ -11,7 +11,9 @@ import ( "time" "github.com/google/uuid" + "github.com/wavetermdev/waveterm/pkg/genconn" "github.com/wavetermdev/waveterm/pkg/wps" + "github.com/wavetermdev/waveterm/pkg/wstore" ) var MainUserInputHandler = UserInputHandler{Channels: make(map[string](chan *UserInputResponse), 1)} @@ -61,10 +63,11 @@ func (ui *UserInputHandler) unregisterChannel(id string) { delete(ui.Channels, id) } -func (ui *UserInputHandler) sendRequestToFrontend(request *UserInputRequest) { +func (ui *UserInputHandler) sendRequestToFrontend(request *UserInputRequest, windowId string) { wps.Broker.Publish(wps.WaveEvent{ - Event: wps.Event_UserInput, - Data: request, + Event: wps.Event_UserInput, + Data: request, + Scopes: []string{windowId}, }) } @@ -74,10 +77,25 @@ func GetUserInput(ctx context.Context, request *UserInputRequest) (*UserInputRes request.RequestId = id deadline, _ := ctx.Deadline() request.TimeoutMs = int(time.Until(deadline).Milliseconds()) - 500 - MainUserInputHandler.sendRequestToFrontend(request) + + connData := genconn.GetConnData(ctx) + // resolve windowId from blockId + tabId, err := wstore.DBFindTabForBlockId(ctx, connData.BlockId) + if err != nil { + return nil, fmt.Errorf("unabled to determine tab for route: %w", err) + } + workspaceId, err := wstore.DBFindWorkspaceForTabId(ctx, tabId) + if err != nil { + return nil, fmt.Errorf("unabled to determine workspace for route: %w", err) + } + windowId, err := wstore.DBFindWindowForWorkspaceId(ctx, workspaceId) + if err != nil { + return nil, fmt.Errorf("unabled to determine window for route: %w", err) + } + + MainUserInputHandler.sendRequestToFrontend(request, windowId) var response *UserInputResponse - var err error select { case resp := <-uiCh: log.Printf("checking received: %v", resp.RequestId) From 0bd62e4ff7ef3e6332a551d441e63437c46cf7b2 Mon Sep 17 00:00:00 2001 From: Sylvia Crowe Date: Tue, 14 Jan 2025 14:17:54 -0800 Subject: [PATCH 4/4] fix: remove debugging wps log --- frontend/app/store/global.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frontend/app/store/global.ts b/frontend/app/store/global.ts index e9bf55de1f..c8a7daae23 100644 --- a/frontend/app/store/global.ts +++ b/frontend/app/store/global.ts @@ -189,7 +189,7 @@ function initGlobalWaveEventSubs(initOpts: WaveInitOpts) { { eventType: "userinput", handler: (event) => { - console.log("userinput event handler", event); + // console.log("userinput event handler", event); const data: UserInputRequest = event.data; modalsModel.pushModal("UserInputModal", { ...data }); },