Skip to content

Commit 46f881c

Browse files
committed
feat: added quickAction notification for async flows
1 parent bca3db9 commit 46f881c

File tree

7 files changed

+58
-4
lines changed

7 files changed

+58
-4
lines changed

runtimes/protocol/chat.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
FILE_CLICK_NOTIFICATION_METHOD,
4040
ChatUpdateParams,
4141
FileClickParams,
42+
QUICK_ACTION_NOTIFICATION_METHOD,
4243
} from './lsp'
4344

4445
export const chatRequestType = new AutoParameterStructuresProtocolRequestType<
@@ -53,11 +54,15 @@ export const endChatRequestType = new ProtocolRequestType<EndChatParams, EndChat
5354
)
5455
export const quickActionRequestType = new AutoParameterStructuresProtocolRequestType<
5556
QuickActionParams | EncryptedQuickActionParams,
56-
QuickActionResult | string | null,
57+
QuickActionResult | string,
5758
QuickActionResult | string,
5859
void,
5960
void
6061
>(QUICK_ACTION_REQUEST_METHOD)
62+
export const quickActionNotificationType = new ProtocolNotificationType<
63+
QuickActionParams | EncryptedQuickActionParams,
64+
void
65+
>(QUICK_ACTION_NOTIFICATION_METHOD)
6166
export const readyNotificationType = new ProtocolNotificationType<void, void>(READY_NOTIFICATION_METHOD)
6267
export const feedbackNotificationType = new ProtocolNotificationType<FeedbackParams, void>(FEEDBACK_NOTIFICATION_METHOD)
6368
export const tabAddNotificationType = new ProtocolNotificationType<TabAddParams, void>(TAB_ADD_NOTIFICATION_METHOD)

runtimes/runtimes/base-runtime.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
selectWorkspaceItemRequestType,
3939
chatUpdateNotificationType,
4040
fileClickNotificationType,
41+
quickActionNotificationType,
4142
} from '../protocol'
4243
import { createConnection } from 'vscode-languageserver/browser'
4344
import {
@@ -126,6 +127,7 @@ export const baseRuntime = (connections: { reader: MessageReader; writer: Messag
126127
onChatPrompt: handler => lspConnection.onRequest(chatRequestType.method, handler),
127128
onEndChat: handler => lspConnection.onRequest(endChatRequestType.method, handler),
128129
onQuickAction: handler => lspConnection.onRequest(quickActionRequestType.method, handler),
130+
onQuickActionTrigger: handler => lspConnection.onNotification(quickActionNotificationType.method, handler),
129131
onSendFeedback: handler => lspConnection.onNotification(feedbackNotificationType.method, handler),
130132
onReady: handler => lspConnection.onNotification(readyNotificationType.method, handler),
131133
onTabAdd: handler => lspConnection.onNotification(tabAddNotificationType.method, handler),

runtimes/runtimes/chat/baseChat.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
chatUpdateNotificationType,
3737
FileClickParams,
3838
fileClickNotificationType,
39+
quickActionNotificationType,
3940
} from '../../protocol'
4041
import { Chat } from '../../server-interface'
4142

@@ -54,6 +55,10 @@ export class BaseChat implements Chat {
5455
this.connection.onRequest(quickActionRequestType.method, handler)
5556
}
5657

58+
public onQuickActionTrigger(handler: NotificationHandler<QuickActionParams>) {
59+
this.connection.onRequest(quickActionNotificationType.method, handler)
60+
}
61+
5762
public onSendFeedback(handler: NotificationHandler<FeedbackParams>) {
5863
this.connection.onNotification(feedbackNotificationType.method, handler)
5964
}

runtimes/runtimes/chat/encryptedChat.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,6 @@ describe('EncryptedChat', () => {
8888
assert(!(result instanceof Error))
8989
sinonAssert.calledOnce(handler)
9090
})
91+
92+
// TODO: Add tests
9193
})

runtimes/runtimes/chat/encryptedChat.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
quickActionRequestType,
1414
ResponseError,
1515
LSPErrorCodes,
16+
NotificationHandler,
17+
quickActionNotificationType,
1618
} from '../../protocol'
1719
import { CredentialsEncoding, encryptObjectWithKey, isMessageJWEEncrypted } from '../auth/standalone/encryption'
1820
import { BaseChat } from './baseChat'
@@ -52,6 +54,42 @@ export class EncryptedChat extends BaseChat {
5254
)
5355
}
5456

57+
public onQuickActionTrigger(handler: NotificationHandler<QuickActionParams>) {
58+
this.registerEncryptedNotificationHandler<EncryptedQuickActionParams, QuickActionParams>(
59+
quickActionNotificationType,
60+
handler
61+
)
62+
}
63+
64+
private registerEncryptedNotificationHandler<
65+
EncryptedRequestType extends EncryptedRequestParams,
66+
DecryptedRequestType extends ChatParams | QuickActionParams,
67+
>(notificationType: any, handler: NotificationHandler<DecryptedRequestType>) {
68+
this.connection.onNotification(
69+
notificationType,
70+
async (request: EncryptedRequestType | DecryptedRequestType, cancellationToken: CancellationToken) => {
71+
// Verify the request is encrypted as expected
72+
if (this.instanceOfEncryptedParams<EncryptedRequestType>(request)) {
73+
// Decrypt request
74+
let decryptedRequest
75+
try {
76+
decryptedRequest = await this.decodeRequest<DecryptedRequestType>(request)
77+
} catch (err: unknown) {
78+
let errorMessage = 'Request could not be decrypted'
79+
if (err instanceof Error) errorMessage = err.message
80+
return new ResponseError<ResponseType>(LSPErrorCodes.ServerCancelled, errorMessage)
81+
}
82+
83+
// Preserve the partial result token
84+
decryptedRequest.partialResultToken = request.partialResultToken
85+
86+
// Call the handler with decrypted params
87+
await handler(decryptedRequest)
88+
}
89+
}
90+
)
91+
}
92+
5593
private registerEncryptedRequestHandler<
5694
EncryptedRequestType extends EncryptedRequestParams,
5795
DecryptedRequestType extends ChatParams | QuickActionParams,

runtimes/server-interface/chat.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type Chat = {
3232
onQuickAction: (handler: RequestHandler<QuickActionParams, QuickActionResult, void>) => void
3333
openTab: (params: OpenTabParams) => Promise<OpenTabResult>
3434
// Notifications
35+
onQuickActionTrigger: (handler: NotificationHandler<QuickActionParams>) => void
3536
onSendFeedback: (handler: NotificationHandler<FeedbackParams>) => void
3637
onReady: (handler: NotificationHandler<void>) => void
3738
onTabAdd: (handler: NotificationHandler<TabAddParams>) => void

types/chat.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Position, Range, TextDocumentIdentifier } from './lsp'
44
export const CHAT_REQUEST_METHOD = 'aws/chat/sendChatPrompt'
55
export const END_CHAT_REQUEST_METHOD = 'aws/chat/endChat'
66
export const QUICK_ACTION_REQUEST_METHOD = 'aws/chat/sendChatQuickAction'
7+
export const QUICK_ACTION_NOTIFICATION_METHOD = 'aws/chat/triggerChatQuickAction'
78
export const READY_NOTIFICATION_METHOD = 'aws/chat/ready'
89
export const FEEDBACK_NOTIFICATION_METHOD = 'aws/chat/feedback'
910
export const TAB_ADD_NOTIFICATION_METHOD = 'aws/chat/tabAdd'
@@ -107,9 +108,10 @@ export type EndChatResult = boolean
107108
*/
108109
export interface QuickActionCommand {
109110
command: string
110-
disabled?: boolean
111111
description?: string
112-
placeholder?: string
112+
disabled?: boolean
113+
async?: boolean
114+
defaultTabData?: TabData
113115
}
114116

115117
/**
@@ -142,7 +144,6 @@ export interface TabData {
142144
*/
143145
export interface ChatOptions {
144146
quickActions?: QuickActions
145-
defaultTabData?: TabData
146147
}
147148

148149
export interface QuickActionParams extends PartialResultParams {

0 commit comments

Comments
 (0)