-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathinlineTask.ts
More file actions
163 lines (140 loc) · 4.99 KB
/
inlineTask.ts
File metadata and controls
163 lines (140 loc) · 4.99 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as vscode from 'vscode'
import type { CodeReference } from 'aws-core-vscode/amazonq'
import type { InlineChatEvent } from 'aws-core-vscode/codewhisperer'
import type { Decorations } from '../decorations/inlineDecorator'
import { computeDecorations } from '../decorations/computeDecorations'
import { extractLanguageNameFromFile } from 'aws-core-vscode/codewhispererChat'
import { textDocumentUtil } from 'aws-core-vscode/shared'
interface TextToInsert {
type: 'insertion'
replacementText: string
range: vscode.Range
}
interface TextToDelete {
type: 'deletion'
originalText: string
range: vscode.Range
}
interface DiffBlock {
originalText: string
replacementText: string
range: vscode.Range
}
export type TextDiff = TextToInsert | TextToDelete
export enum TaskState {
Idle = 'Idle',
InProgress = 'InProgress',
WaitingForDecision = 'WaitingForDecision',
Complete = 'Complete',
Error = 'Error',
}
export class InlineTask {
public state: TaskState = TaskState.Idle
public diff: TextDiff[] = []
public decorations: Decorations | undefined
public diffBlock: DiffBlock[] = []
public codeReferences: CodeReference[] = []
public selectedText: string
public languageName: string | undefined
public readonly originalDocumentText: string
public partialSelectedText: string | undefined
public partialSelectedTextRight: string | undefined
public previouseDiff: TextDiff[] | undefined
public selectedRange: vscode.Range
public inProgressReplacement: string | undefined
public replacement: string | undefined
// Telemetry fields
public requestId?: string
public responseStartLatency?: number
public responseEndLatency?: number
constructor(
public query: string,
public document: vscode.TextDocument,
selection: vscode.Selection
) {
this.selectedRange = textDocumentUtil.expandSelectionToFullLines(document, selection)
this.selectedText = document.getText(this.selectedRange)
this.originalDocumentText = document.getText()
this.languageName = extractLanguageNameFromFile(document)
}
public revertDiff(): void {
this.diff = []
this.decorations = {
linesAdded: [],
linesRemoved: [],
}
}
public removeDiffChangeByRange(range: vscode.Range): void {
if (this.diff) {
this.diff = this.diff.filter((change) => !change.range.isEqual(range))
}
}
public updateDecorations(): void {
const isEmpty =
!this.decorations ||
(this.decorations?.linesAdded?.length === 0 && this.decorations?.linesRemoved?.length === 0)
if (isEmpty) {
return
}
const updatedDecorations = computeDecorations(this)
this.decorations = updatedDecorations
}
public updateDiff(affectedRange: vscode.Range, deletedLines: number) {
const diffsAfter = this.diff.filter((edit) => edit.range.start.isAfter(affectedRange.end))
for (const diff of diffsAfter) {
diff.range = new vscode.Range(
diff.range.start.translate(-deletedLines),
diff.range.end.translate(-deletedLines)
)
}
}
// Telemetry methods
public get numSelectedLines() {
return this.selectedText.split('\n').length
}
public get inputLength() {
return this.query.length
}
public inlineChatEventBase() {
let numSuggestionAddChars = 0
let numSuggestionAddLines = 0
let numSuggestionDelChars = 0
let numSuggestionDelLines = 0
for (const diff of this.diff) {
if (diff.type === 'insertion') {
numSuggestionAddChars += diff.replacementText.length
numSuggestionAddLines += diff.range.end.line - diff.range.start.line + 1
} else {
numSuggestionDelChars += diff.originalText.length
numSuggestionDelLines += diff.range.end.line - diff.range.start.line + 1
}
}
const programmingLanguage = this.languageName
? {
languageName: this.languageName,
}
: undefined
const event: Partial<InlineChatEvent> = {
requestId: this.requestId,
timestamp: new Date(),
inputLength: this.inputLength,
numSelectedLines: this.numSelectedLines,
codeIntent: true,
responseStartLatency: this.responseStartLatency,
responseEndLatency: this.responseEndLatency,
numSuggestionAddChars,
numSuggestionAddLines,
numSuggestionDelChars,
numSuggestionDelLines,
programmingLanguage,
}
return event
}
public isActiveState() {
return !(this.state === TaskState.Complete || this.state === TaskState.Error)
}
}