Skip to content

Commit 8c9cac7

Browse files
authored
feat(types): add protocol for open file dialog for import image as context (#564)
* feat(types): add protocol for open file dialog * feat(types): register open file dialog events * feat(types): add drag and drop uiContract
1 parent 3ed3d59 commit 8c9cac7

File tree

10 files changed

+96
-4
lines changed

10 files changed

+96
-4
lines changed

chat-client-ui-types/src/uiContracts.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
GET_SERIALIZED_CHAT_REQUEST_METHOD,
99
GetSerializedChatResult,
1010
ChatPrompt,
11+
OpenFileDialogParams,
12+
DropFilesParams,
1113
} from '@aws/language-server-runtimes-types'
1214
export { InsertToCursorPositionParams } from '@aws/language-server-runtimes-types'
1315

@@ -30,7 +32,8 @@ export const DISCLAIMER_ACKNOWLEDGED = 'disclaimerAcknowledged'
3032
export const CHAT_PROMPT_OPTION_ACKNOWLEDGED = 'chatPromptOptionAcknowledged'
3133
export const STOP_CHAT_RESPONSE = 'stopChatResponse'
3234
export const OPEN_SETTINGS = 'openSettings'
33-
35+
export const OPEN_FILE_DIALOG = 'openFileDialog'
36+
export const FILES_DROPPED = 'filesDropped'
3437
/**
3538
* A message sent from Chat Client to Extension in response to various actions triggered from Chat UI.
3639
*/
@@ -51,6 +54,8 @@ export type UiMessageCommand =
5154
| typeof CHAT_PROMPT_OPTION_ACKNOWLEDGED
5255
| typeof STOP_CHAT_RESPONSE
5356
| typeof OPEN_SETTINGS
57+
| typeof OPEN_FILE_DIALOG
58+
| typeof FILES_DROPPED
5459

5560
export type UiMessageParams =
5661
| InsertToCursorPositionParams
@@ -63,6 +68,8 @@ export type UiMessageParams =
6368
| ChatPromptOptionAcknowledgedParams
6469
| StopChatResponseParams
6570
| OpenSettingsParams
71+
| OpenFileDialogParams
72+
| DropFilesParams
6673

6774
export interface SendToPromptParams {
6875
selection: string
@@ -159,6 +166,12 @@ export interface OpenSettingsParams {
159166
settingKey: string
160167
}
161168

169+
export interface FilesDroppedParams {
170+
tabId: string
171+
insertPosition: number
172+
files: string[]
173+
}
174+
162175
/**
163176
* A message sent from Chat Client to Extension in response to request triggered from Extension.
164177
* As Chat Client uses PostMessage API for transport with integrating Extensions, this is a loose implementation of request-response model.

runtimes/protocol/chat.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ import {
8585
PINNED_CONTEXT_REMOVE_NOTIFICATION_METHOD,
8686
ActiveEditorChangedParams,
8787
ACTIVE_EDITOR_CHANGED_NOTIFICATION_METHOD,
88+
OPEN_FILE_DIALOG_METHOD,
89+
OpenFileDialogParams,
90+
OpenFileDialogResult,
8891
} from './lsp'
8992

9093
export const chatRequestType = new AutoParameterStructuresProtocolRequestType<
@@ -94,6 +97,13 @@ export const chatRequestType = new AutoParameterStructuresProtocolRequestType<
9497
void,
9598
void
9699
>(CHAT_REQUEST_METHOD)
100+
export const openFileDialogRequestType = new AutoParameterStructuresProtocolRequestType<
101+
OpenFileDialogParams,
102+
OpenFileDialogResult,
103+
OpenFileDialogResult,
104+
void,
105+
void
106+
>(OPEN_FILE_DIALOG_METHOD)
97107
export const inlineChatRequestType = new AutoParameterStructuresProtocolRequestType<
98108
InlineChatParams | EncryptedChatParams,
99109
InlineChatResult | string,

runtimes/protocol/window.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {
33
SHOW_SAVE_FILE_DIALOG_REQUEST_METHOD,
44
ShowSaveFileDialogParams,
55
ShowSaveFileDialogResult,
6+
ShowOpenDialogResult,
7+
ShowOpenDialogParams,
8+
SHOW_OPEN_FILE_DIALOG_REQUEST_METHOD,
69
} from './lsp'
710

811
/**
@@ -16,3 +19,11 @@ export const ShowSaveFileDialogRequestType = new ProtocolRequestType<
1619
void,
1720
void
1821
>(SHOW_SAVE_FILE_DIALOG_REQUEST_METHOD)
22+
23+
export const ShowOpenDialogRequestType = new ProtocolRequestType<
24+
ShowOpenDialogParams,
25+
ShowOpenDialogResult,
26+
never,
27+
void,
28+
void
29+
>(SHOW_OPEN_FILE_DIALOG_REQUEST_METHOD)

runtimes/runtimes/base-runtime.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
ShowMessageNotification,
3434
ShowMessageRequest,
3535
ShowDocumentRequest,
36+
ShowOpenDialogRequestType,
3637
openTabRequestType,
3738
openFileDiffNotificationType,
3839
selectWorkspaceItemRequestType,
@@ -57,6 +58,7 @@ import {
5758
activeEditorChangedNotificationType,
5859
onPinnedContextAddNotificationType,
5960
onPinnedContextRemoveNotificationType,
61+
openFileDialogRequestType,
6062
} from '../protocol'
6163
import { createConnection } from 'vscode-languageserver/browser'
6264
import {
@@ -190,6 +192,7 @@ export const baseRuntime = (connections: { reader: MessageReader; writer: Messag
190192
onTabBarAction: handler => lspConnection.onRequest(tabBarActionRequestType.method, handler),
191193
onPromptInputOptionChange: handler =>
192194
lspConnection.onNotification(promptInputOptionChangeNotificationType.method, handler),
195+
onOpenFileDialog: handler => lspConnection.onRequest(openFileDialogRequestType.method, handler),
193196
onRuleClick: handler => lspConnection.onRequest(ruleClickRequestType.method, handler),
194197
}
195198

@@ -258,6 +261,7 @@ export const baseRuntime = (connections: { reader: MessageReader; writer: Messag
258261
showMessageRequest: params => lspConnection.sendRequest(ShowMessageRequest.method, params),
259262
showDocument: params => lspConnection.sendRequest(ShowDocumentRequest.method, params),
260263
showSaveFileDialog: params => lspConnection.sendRequest(ShowSaveFileDialogRequestType.method, params),
264+
showOpenDialog: params => lspConnection.sendRequest(ShowOpenDialogRequestType.method, params),
261265
},
262266
publishDiagnostics: params => lspConnection.sendNotification(PublishDiagnosticsNotification.method, params),
263267
sendProgress: <P>(type: ProgressType<P>, token: ProgressToken, value: P) => {

runtimes/runtimes/chat/baseChat.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ import {
8282
activeEditorChangedNotificationType,
8383
onPinnedContextAddNotificationType,
8484
onPinnedContextRemoveNotificationType,
85+
OpenFileDialogParams,
86+
OpenFileDialogResult,
87+
openFileDialogRequestType,
8588
} from '../../protocol'
8689
import { Chat } from '../../server-interface'
8790

@@ -92,6 +95,10 @@ export class BaseChat implements Chat {
9295
this.connection.onRequest(chatRequestType.method, handler)
9396
}
9497

98+
public onOpenFileDialog(handler: RequestHandler<OpenFileDialogParams, OpenFileDialogResult, OpenFileDialogResult>) {
99+
this.connection.onRequest(openFileDialogRequestType.method, handler)
100+
}
101+
95102
public onInlineChatPrompt(
96103
handler: RequestHandler<InlineChatParams, InlineChatResult | null | undefined, InlineChatResult>
97104
) {

runtimes/runtimes/standalone.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828
didWriteFileNotificationType,
2929
didAppendFileNotificationType,
3030
didCreateDirectoryNotificationType,
31+
ShowOpenDialogParams,
32+
ShowOpenDialogRequestType,
3133
} from '../protocol'
3234
import { ProposedFeatures, createConnection } from 'vscode-languageserver/node'
3335
import {
@@ -393,6 +395,8 @@ export const standalone = (props: RuntimeProps) => {
393395
showDocument: params => lspConnection.sendRequest(ShowDocumentRequest.method, params),
394396
showSaveFileDialog: (params: ShowSaveFileDialogParams) =>
395397
lspConnection.sendRequest(ShowSaveFileDialogRequestType.method, params),
398+
showOpenDialog: (params: ShowOpenDialogParams) =>
399+
lspConnection.sendRequest(ShowOpenDialogRequestType.method, params),
396400
},
397401
publishDiagnostics: params =>
398402
lspConnection.sendNotification(PublishDiagnosticsNotification.method, params),

runtimes/server-interface/chat.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import {
4141
ListMcpServersResult,
4242
McpServerClickResult,
4343
McpServerClickParams,
44+
OpenFileDialogParams,
45+
OpenFileDialogResult,
4446
RuleClickParams,
4547
ListRulesParams,
4648
ListRulesResult,
@@ -70,7 +72,9 @@ export type Chat = {
7072
getSerializedChat: (params: GetSerializedChatParams) => Promise<GetSerializedChatResult>
7173
onListRules: (handler: RequestHandler<ListRulesParams, ListRulesResult, void>) => void
7274
onRuleClick: (handler: RequestHandler<RuleClickParams, RuleClickResult, void>) => void
73-
75+
onOpenFileDialog: (
76+
handler: RequestHandler<OpenFileDialogParams, OpenFileDialogResult, OpenFileDialogResult>
77+
) => void
7478
// Notifications
7579
onSendFeedback: (handler: NotificationHandler<FeedbackParams>) => void
7680
onReady: (handler: NotificationHandler<void>) => void

runtimes/server-interface/lsp.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ import {
5959
SelectWorkspaceItemResult,
6060
ShowSaveFileDialogParams,
6161
ShowSaveFileDialogResult,
62+
ShowOpenDialogParams,
63+
ShowOpenDialogResult,
6264
} from '../protocol'
6365

6466
// Re-export whole surface of LSP protocol used in Runtimes.
@@ -151,6 +153,7 @@ export type Lsp = {
151153
showMessageRequest: (params: ShowMessageRequestParams) => Promise<MessageActionItem | null>
152154
showDocument: (params: ShowDocumentParams) => Promise<ShowDocumentResult>
153155
showSaveFileDialog: (params: ShowSaveFileDialogParams) => Promise<ShowSaveFileDialogResult>
156+
showOpenDialog: (params: ShowOpenDialogParams) => Promise<ShowOpenDialogResult>
154157
}
155158
extensions: {
156159
onInlineCompletionWithReferences: (

types/chat.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const INLINE_CHAT_REQUEST_METHOD = 'aws/chat/sendInlineChatPrompt'
2222
export const TAB_BAR_ACTION_REQUEST_METHOD = 'aws/chat/tabBarAction'
2323
export const CHAT_OPTIONS_UPDATE_NOTIFICATION_METHOD = 'aws/chat/chatOptionsUpdate'
2424
export const PROMPT_INPUT_OPTION_CHANGE_METHOD = 'aws/chat/promptInputOptionChange'
25+
export const OPEN_FILE_DIALOG_METHOD = 'aws/chat/openFileDialog'
2526
// context
2627
export const CONTEXT_COMMAND_NOTIFICATION_METHOD = 'aws/chat/sendContextCommands'
2728
export const CREATE_PROMPT_NOTIFICATION_METHOD = 'aws/chat/createPrompt'
@@ -417,7 +418,7 @@ export interface ContextCommandGroup {
417418
export interface ContextCommand extends QuickActionCommand {
418419
id?: string
419420
route?: string[]
420-
label?: 'file' | 'folder' | 'code'
421+
label?: 'file' | 'folder' | 'code' | 'image'
421422
children?: ContextCommandGroup[]
422423
}
423424

@@ -436,6 +437,27 @@ export interface CreatePromptParams {
436437
isRule?: boolean
437438
}
438439

440+
export interface OpenFileDialogParams {
441+
tabId: string
442+
fileType: 'image' | ''
443+
insertPosition: number
444+
}
445+
446+
export interface OpenFileDialogResult {
447+
tabId: string
448+
fileType: 'image' | ''
449+
filePaths: string[]
450+
errorMessage?: string
451+
insertPosition: number
452+
}
453+
454+
export interface DropFilesParams {
455+
tabId: string
456+
files: FileList
457+
insertPosition: number
458+
errorMessage?: string
459+
}
460+
439461
export interface ProgrammingLanguage {
440462
languageName: string
441463
}

types/window.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { URI } from 'vscode-languageserver-types'
22

33
export const SHOW_SAVE_FILE_DIALOG_REQUEST_METHOD = 'aws/showSaveFileDialog'
4-
4+
export const SHOW_OPEN_FILE_DIALOG_REQUEST_METHOD = 'aws/showOpenFileDialog'
55
export interface ShowSaveFileDialogParams {
66
// Using untyped string to avoid locking this too strictly.
77
// TODO: Migrate to LanguageKind when it is released in 3.18.0
@@ -13,3 +13,17 @@ export interface ShowSaveFileDialogParams {
1313
export interface ShowSaveFileDialogResult {
1414
targetUri: URI
1515
}
16+
17+
// bridge to consume ide's api
18+
export interface ShowOpenDialogParams {
19+
canSelectFiles?: boolean
20+
canSelectFolders?: boolean
21+
canSelectMany?: boolean
22+
filters?: { [key: string]: string[] }
23+
defaultUri?: URI
24+
title?: string
25+
}
26+
27+
export interface ShowOpenDialogResult {
28+
uris: URI[]
29+
}

0 commit comments

Comments
 (0)