Skip to content

Commit f4fa53f

Browse files
authored
fix(vscode): on startup pick the right model (#4556)
1 parent 24c9b4e commit f4fa53f

9 files changed

Lines changed: 225 additions & 99 deletions

File tree

vscode/bus/src/callbacks.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1+
import type { Result } from './result'
2+
3+
export type CallbackShape = Record<string, any>
4+
15
export type Callback = {
26
openFile: {
37
uri: string
48
}
5-
queryRequest: {
6-
requestId: string
7-
url: string
8-
method?: string
9-
params?: Record<string, string>
10-
body?: Record<string, any>
11-
}
12-
}
9+
rpcResponse: RPCResponse
10+
} & CallbackShape
1311

1412
/**
1513
* A tuple type representing a callback event with its associated payload.
@@ -23,22 +21,52 @@ export type CallbackEvent = {
2321
[K in keyof Callback]: { key: K; payload: Callback[K] }
2422
}[keyof Callback]
2523

24+
export type VSCodeCallbackShape = Record<string, any>
25+
2626
/**
2727
* A tuple type representing a VSCode event with its associated payload.
2828
*/
2929
export type VSCodeCallback = {
3030
changeFocusOnFile: {
3131
path: string
3232
}
33-
queryResponse: {
34-
requestId: string
35-
response: any
36-
}
3733
savedFile: {
3834
fileUri: string
3935
}
40-
}
36+
rpcRequest: RPCRequest
37+
} & VSCodeCallbackShape
4138

4239
export type VSCodeEvent = {
4340
[K in keyof VSCodeCallback]: { key: K; payload: VSCodeCallback[K] }
4441
}[keyof VSCodeCallback]
42+
43+
type RPCMethodsShape = Record<string, { params: any; result: any }>
44+
45+
export type RPCMethods = {
46+
get_active_file: {
47+
params: {}
48+
result: {
49+
fileUri?: string
50+
}
51+
}
52+
api_query: {
53+
params: {
54+
url: string
55+
method: string
56+
params: any
57+
body: any
58+
}
59+
result: any
60+
}
61+
} & RPCMethodsShape
62+
63+
export type RPCRequest = {
64+
requestId: string
65+
method: keyof RPCMethods
66+
params: RPCMethods[keyof RPCMethods]['params']
67+
}
68+
69+
export type RPCResponse = {
70+
requestId: string
71+
result: Result<RPCMethods[keyof RPCMethods]['result'], string>
72+
}

vscode/extension/src/webviews/lineagePanel.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CallbackEvent } from '@bus/callbacks'
1+
import { CallbackEvent, RPCRequest } from '@bus/callbacks'
22
import {
33
Disposable,
44
Uri,
@@ -83,18 +83,43 @@ export class LineagePanel implements WebviewViewProvider, Disposable {
8383
await window.showTextDocument(document)
8484
break
8585
}
86-
case 'queryRequest': {
87-
const payload = message.payload
88-
const requestId = message.payload.requestId
89-
const response = await this.lsp.call_custom_method(
90-
'sqlmesh/api',
91-
payload as any,
92-
)
93-
webviewView.webview.postMessage({
94-
key: 'query_response',
95-
payload: response,
96-
requestId,
97-
})
86+
case 'rpcRequest': {
87+
const payload: RPCRequest = message.payload
88+
const requestId = payload.requestId
89+
switch (payload.method) {
90+
case 'api_query': {
91+
const response = await this.lsp.call_custom_method(
92+
'sqlmesh/api',
93+
payload.params,
94+
)
95+
const responseCallback: CallbackEvent = {
96+
key: 'rpcResponse',
97+
payload: {
98+
requestId,
99+
result: response,
100+
},
101+
}
102+
webviewView.webview.postMessage(responseCallback)
103+
break
104+
}
105+
case 'get_active_file': {
106+
const active_file = window.activeTextEditor?.document.uri.fsPath
107+
const responseCallback: CallbackEvent = {
108+
key: 'rpcResponse',
109+
payload: {
110+
requestId,
111+
result: {
112+
fileUri: active_file,
113+
},
114+
},
115+
}
116+
webviewView.webview.postMessage(responseCallback)
117+
break
118+
}
119+
default: {
120+
throw new Error(`Unhandled RPC method: ${payload.method}`)
121+
}
122+
}
98123
break
99124
}
100125
default:

vscode/react/src/api/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ export function useApiModelLineage(
7070
return useQuery({
7171
queryKey: ['/api/lineage', modelName],
7272
queryFn: async ({ signal }) => {
73-
return await modelLineageApiLineageModelNameGet(modelName, { signal })
73+
try {
74+
const response = await modelLineageApiLineageModelNameGet(modelName, {
75+
signal,
76+
})
77+
return response
78+
} catch (error) {
79+
console.error('error fetching lineage', error)
80+
throw error
81+
}
7482
},
7583
})
7684
}

vscode/react/src/api/instance.ts

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sendVSCodeMessage } from '@/utils/vscodeapi'
1+
import { callRpc } from '@/utils/rpc'
22
import { isErr } from '@bus/result'
33

44
declare global {
@@ -38,43 +38,16 @@ export async function fetchAPI<T = any, B extends object = any>(
3838
config: FetchOptions<B>,
3939
_options?: Partial<FetchOptionsWithSignal>,
4040
): Promise<T & ResponseWithDetail> {
41-
// Generate a unique ID for this request
42-
// Create a promise that will resolve when we get a response with matching ID
43-
return new Promise((resolve, reject) => {
44-
const requestId = `query_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
45-
const messageHandler = (event: MessageEvent) => {
46-
if (
47-
event.data &&
48-
event.data.key === 'query_response' &&
49-
event.data.requestId === requestId
50-
) {
51-
// Remove the listener once we get our response
52-
window.removeEventListener('message', messageHandler)
53-
54-
const payload = event.data.payload
55-
if (isErr(payload)) {
56-
reject(new Error(payload.error as string))
57-
} else {
58-
resolve(payload.value.data)
59-
}
60-
}
61-
}
62-
63-
// Add the listener
64-
window.addEventListener('message', messageHandler)
65-
66-
sendVSCodeMessage('queryRequest', {
67-
requestId,
68-
url: config.url,
69-
params: config.params as any,
70-
body: config.data,
71-
method: config.method,
72-
})
73-
74-
// Set a timeout to prevent hanging promises
75-
setTimeout(() => {
76-
window.removeEventListener('message', messageHandler)
77-
reject(new Error('Query request timed out'))
78-
}, 30000) // 30 second timeout
79-
})
41+
const request = {
42+
url: config.url,
43+
method: config.method,
44+
params: config.params,
45+
body: config.data,
46+
}
47+
const result = await callRpc('api_query', request)
48+
if (isErr(result)) {
49+
throw new Error(result.error)
50+
}
51+
const response = result.value.data
52+
return response
8053
}

vscode/react/src/components/graph/ModelLineage.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function ModelLineage({
7878
useApiModelLineage(model.name)
7979
const { isFetching: isFetchingModels } = useApiModels()
8080

81-
const [isMegringModels, setIsMergingModels] = useState(false)
81+
const [isMergingModels, setIsMergingModels] = useState(false)
8282
const [modelLineage, setModelLineage] = useState<
8383
ModelLineageApiLineageModelNameGet200 | undefined
8484
>(undefined)
@@ -91,7 +91,6 @@ export function ModelLineage({
9191
getModelLineage()
9292
.then(({ data }) => {
9393
setModelLineage(data)
94-
9594
if (isNil(data)) return
9695

9796
setIsMergingModels(true)
@@ -164,7 +163,7 @@ export function ModelLineage({
164163
}
165164

166165
const isFetching =
167-
isFetchingModelLineage || isFetchingModels || isMegringModels
166+
isFetchingModelLineage || isFetchingModels || isMergingModels
168167

169168
return (
170169
<div className="relative h-full w-full overflow-hidden">

vscode/react/src/components/graph/context.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export const LineageFlowContext = createContext<LineageFlow>({
104104
setActiveEdges: () => {},
105105
handleClickModel: () => {},
106106
setManuallySelectedColumn: () => {},
107-
handleError: () => {},
107+
handleError: error => console.error(error),
108108
setLineage: () => {},
109109
setLineageCache: () => {},
110110
isActiveColumn: () => false,

vscode/react/src/components/graph/help.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ function getNodeMap({
176176
? decodeURI(modelName)
177177
: modelName
178178
const model = Object.values(models).find(m => m.fqn === decodedModelName)
179-
console.log('model', model)
180179
const nodeType: LineageNodeModelType = isNotNil(model)
181180
? (model.type as LineageNodeModelType)
182181
: // If model name present in lineage but not in global models

0 commit comments

Comments
 (0)