forked from wso2/vscode-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPCLayer.ts
More file actions
125 lines (111 loc) · 5.74 KB
/
RPCLayer.ts
File metadata and controls
125 lines (111 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com) All Rights Reserved.
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { WebviewView, WebviewPanel, window, QuickPickItem, commands } from 'vscode';
import { Messenger } from 'vscode-messenger';
import { StateMachine } from './stateMachine';
import { COMMANDS } from './constants';
import { stateChanged, getVisualizerState, VisualizerLocation, getPopupVisualizerState, PopupVisualizerLocation, popupStateChanged, selectQuickPickItem, WebviewQuickPickItem, selectQuickPickItems, showConfirmMessage, showInputBox, showInfoNotification, showErrorNotification, onTraceEvent, onMCPStateChange, MCPStateChangeEvent, focusOverviewPanel, EVENT_TYPE, MACHINE_VIEW } from '@wso2/arazzo-designer-core';
import { openView } from './stateMachine';
import { TracerServer } from './mcp/tracing';
import { VisualizerWebview } from './visualizer/webview';
import { StateMachinePopup } from './stateMachinePopup';
import path = require('path');
import { registerVisualizerRpcHandlers } from './rpc-managers/visualizer/rpc-handler';
export class RPCLayer {
static _messenger: Messenger = new Messenger();
constructor(webViewPanel: WebviewPanel | WebviewView) {
if (isWebviewPanel(webViewPanel)) {
RPCLayer._messenger.registerWebviewPanel(webViewPanel as WebviewPanel);
StateMachine.service().onTransition((state) => {
RPCLayer._messenger.sendNotification(stateChanged, { type: 'webview', webviewType: VisualizerWebview.viewType }, state.value);
});
// Form machine transition
StateMachinePopup.service().onTransition((state) => {
RPCLayer._messenger.sendNotification(popupStateChanged, { type: 'webview', webviewType: VisualizerWebview.viewType }, state.value);
});
} else {
RPCLayer._messenger.registerWebviewPanel(webViewPanel as WebviewPanel);
}
}
static create(webViewPanel: WebviewPanel | WebviewView) {
return new RPCLayer(webViewPanel);
}
static init() {
// ----- Main Webview RPC Methods
RPCLayer._messenger.onRequest(getVisualizerState, () => getContext());
registerVisualizerRpcHandlers(RPCLayer._messenger);
// ----- Popup Views RPC Methods
RPCLayer._messenger.onRequest(getPopupVisualizerState, () => getPopupContext());
// ----- VScode interactions RPC Methods
RPCLayer._messenger.onRequest(selectQuickPickItem, async (params) => {
const itemSelection = await window.showQuickPick(params.items as QuickPickItem[], { title: params.title, placeHolder: params.placeholder });
return itemSelection as WebviewQuickPickItem;
});
RPCLayer._messenger.onRequest(selectQuickPickItems, async (params) => {
const itemSelection = await window.showQuickPick(params.items as QuickPickItem[], { title: params.title, placeHolder: params.placeholder, canPickMany: true });
return itemSelection as WebviewQuickPickItem[];
});
RPCLayer._messenger.onRequest(showConfirmMessage, async (params) => {
const response = await window.showInformationMessage(params.message, { modal: true }, params.buttonText);
return response === params.buttonText;
});
RPCLayer._messenger.onRequest(showInputBox, async (params) => window.showInputBox(params));
RPCLayer._messenger.onNotification(showInfoNotification, (message) => {
window.showInformationMessage(message);
});
RPCLayer._messenger.onNotification(showErrorNotification, (message) => {
window.showErrorMessage(message);
});
// Handle focus-overview-panel request from the workflow panel webview
RPCLayer._messenger.onNotification(focusOverviewPanel, (fileUri: string) => {
commands.executeCommand(COMMANDS.OPEN_WELCOME, fileUri);
});
// Forward trace events from the tracer server to the webview
TracerServer.getInstance().onEvent((event) => {
RPCLayer._messenger.sendNotification(onTraceEvent, { type: 'webview', webviewType: VisualizerWebview.viewType }, event as any);
});
}
/** Broadcast the current MCP server state to the webview. */
static sendMCPStateChange(state: MCPStateChangeEvent): void {
RPCLayer._messenger.sendNotification(onMCPStateChange, { type: 'webview', webviewType: VisualizerWebview.viewType }, state);
}
}
async function getContext(): Promise<VisualizerLocation> {
const context = StateMachine.context();
return new Promise((resolve) => {
resolve({
documentUri: context.documentUri,
view: context.view,
identifier: context.identifier,
projectUri: context.projectUri
});
});
}
async function getPopupContext(): Promise<PopupVisualizerLocation> {
const context = StateMachinePopup.context();
return new Promise((resolve) => {
resolve({
documentUri: context.documentUri,
view: context.view,
recentIdentifier: context.recentIdentifier
});
});
}
function isWebviewPanel(webview: WebviewPanel | WebviewView): boolean {
return webview.viewType === VisualizerWebview.viewType;
}