-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathsessionManager.ts
102 lines (89 loc) · 3.06 KB
/
sessionManager.ts
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
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import { InlineCompletionItemWithReferences } from '@aws/language-server-runtimes-types'
// TODO: add more needed data to the session interface
interface CodeWhispererSession {
sessionId: string
suggestions: InlineCompletionItemWithReferences[]
// TODO: might need to convert to enum states
isRequestInProgress: boolean
requestStartTime: number
firstCompletionDisplayLatency?: number
}
export class SessionManager {
private activeSession?: CodeWhispererSession
private activeIndex: number = 0
constructor() {}
public startSession(
sessionId: string,
suggestions: InlineCompletionItemWithReferences[],
requestStartTime: number,
firstCompletionDisplayLatency?: number
) {
this.activeSession = {
sessionId,
suggestions,
isRequestInProgress: true,
requestStartTime,
firstCompletionDisplayLatency,
}
this.activeIndex = 0
}
public closeSession() {
if (!this.activeSession) {
return
}
this.activeSession.isRequestInProgress = false
}
public getActiveSession() {
return this.activeSession
}
public updateSessionSuggestions(suggestions: InlineCompletionItemWithReferences[]) {
if (!this.activeSession) {
return
}
this.activeSession.suggestions = [...this.activeSession.suggestions, ...suggestions]
}
public incrementActiveIndex() {
const suggestionCount = this.activeSession?.suggestions?.length
if (!suggestionCount) {
return
}
this.activeIndex === suggestionCount - 1 ? suggestionCount - 1 : this.activeIndex++
}
public decrementActiveIndex() {
this.activeIndex === 0 ? 0 : this.activeIndex--
}
/*
We have to maintain the active suggestion index ourselves because VS Code doesn't expose which suggestion it's currently showing
In order to keep track of the right suggestion state, and for features such as reference tracker, this hack is still needed
*/
public getActiveRecommendation(): InlineCompletionItemWithReferences[] {
let suggestionCount = this.activeSession?.suggestions.length
if (!suggestionCount) {
return []
}
if (suggestionCount === 1 && this.activeSession?.isRequestInProgress) {
suggestionCount += 1
}
const activeSuggestion = this.activeSession?.suggestions[this.activeIndex]
if (!activeSuggestion) {
return []
}
const items = [activeSuggestion]
// to make the total number of suggestions match the actual number
for (let i = 1; i < suggestionCount; i++) {
items.push({
...activeSuggestion,
insertText: `${i}`,
})
}
return items
}
public clear() {
this.activeSession = undefined
this.activeIndex = 0
}
}