-
Notifications
You must be signed in to change notification settings - Fork 201
/
protocol.ts
286 lines (263 loc) · 8.33 KB
/
protocol.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import type { URI } from 'vscode-uri'
import type {
AuthStatus,
ChatMessage,
ConfigurationWithAccessToken,
ContextItem,
EnhancedContextContextT,
ModelProvider,
RangeData,
SearchPanelFile,
TelemetryEventProperties,
UserLocalHistory,
} from '@sourcegraph/cody-shared'
import type { CodeBlockMeta } from '../../webviews/chat/CodeBlocks'
import type { View } from '../../webviews/NavBar'
import type { Repo } from '../context/repo-fetcher'
/**
* A message sent from the webview to the extension host.
*/
export type WebviewMessage =
| { command: 'ready' }
| { command: 'initialized' }
| {
command: 'event'
eventName: string
properties: TelemetryEventProperties | undefined
} // new event log internal API (use createWebviewTelemetryService wrapper)
| ({ command: 'submit' } & WebviewSubmitMessage)
| { command: 'history'; action: 'clear' | 'export' }
| { command: 'restoreHistory'; chatID: string }
| { command: 'deleteHistory'; chatID: string }
| { command: 'links'; value: string }
| {
command: 'show-page'
page: string
}
| { command: 'chatModel'; model: string }
| { command: 'get-chat-models' }
| {
command: 'openFile'
uri: URI
range?: RangeData
}
| {
command: 'openLocalFileWithRange'
filePath: string
// Note: we're not using vscode.Range objects or nesting here, as the protocol
// tends to munge the type in a weird way (nested fields become array indices).
range?: RangeData
}
| ({ command: 'edit' } & WebviewEditMessage)
| { command: 'context/get-remote-search-repos' }
| { command: 'context/choose-remote-search-repo'; explicitRepos?: Repo[] }
| { command: 'context/remove-remote-search-repo'; repoId: string }
| { command: 'embeddings/index' }
| { command: 'symf/index' }
| { command: 'insert'; text: string; metadata?: CodeBlockMeta }
| { command: 'newFile'; text: string; metadata?: CodeBlockMeta }
| {
command: 'copy'
eventType: 'Button' | 'Keydown'
text: string
metadata?: CodeBlockMeta
}
| {
command: 'auth'
authKind: 'signin' | 'signout' | 'support' | 'callback' | 'simplified-onboarding'
endpoint?: string
value?: string
authMethod?: AuthMethod
}
| { command: 'abort' }
| { command: 'reload' }
| {
command: 'simplified-onboarding'
onboardingKind: 'web-sign-in-token'
}
| { command: 'getUserContext'; query: string }
| { command: 'search'; query: string }
| {
command: 'show-search-result'
uri: URI
range: RangeData
}
| {
command: 'reset'
}
| {
command: 'attribution-search'
snippet: string
}
/**
* A message sent from the extension host to the webview.
*/
export type ExtensionMessage =
| {
type: 'config'
config: ConfigurationSubsetForWebview & LocalEnv
authStatus: AuthStatus
workspaceFolderUris: string[]
}
| {
type: 'search:config'
workspaceFolderUris: string[]
}
| { type: 'history'; localHistory: UserLocalHistory | null }
| ({ type: 'transcript' } & ExtensionTranscriptMessage)
| { type: 'view'; view: View }
| { type: 'errors'; errors: string }
| { type: 'notice'; notice: { key: string } }
| { type: 'transcript-errors'; isTranscriptError: boolean }
| {
type: 'userContextFiles'
userContextFiles: ContextItem[] | null
}
| { type: 'chatModels'; models: ModelProvider[] }
| {
type: 'update-search-results'
results: SearchPanelFile[]
query: string
}
| { type: 'index-updated'; scopeDir: string }
| { type: 'enhanced-context'; enhancedContextStatus: EnhancedContextContextT }
| ({ type: 'attribution' } & ExtensionAttributionMessage)
| { type: 'setChatEnabledConfigFeature'; data: boolean }
| { type: 'webview-state'; isActive: boolean }
| { type: 'context/remote-repos'; repos: Repo[] }
| {
type: 'setConfigFeatures'
configFeatures: {
chat: boolean
attribution: boolean
}
}
interface ExtensionAttributionMessage {
snippet: string
attribution?: {
repositoryNames: string[]
limitHit: boolean
}
error?: string
}
export type ChatSubmitType = 'user' | 'user-newchat'
export interface WebviewSubmitMessage extends WebviewContextMessage {
text: string
submitType: ChatSubmitType
}
interface WebviewEditMessage extends WebviewContextMessage {
text: string
index?: number
}
interface WebviewContextMessage {
addEnhancedContext?: boolean
contextFiles?: ContextItem[]
}
export interface ExtensionTranscriptMessage {
messages: ChatMessage[]
isMessageInProgress: boolean
chatID: string
}
/**
* The subset of configuration that is visible to the webview.
*/
export interface ConfigurationSubsetForWebview
extends Pick<
ConfigurationWithAccessToken,
'debugEnable' | 'experimentalGuardrails' | 'serverEndpoint'
> {}
/**
* URLs for the Sourcegraph instance and app.
*/
export const CODY_DOC_URL = new URL('https://sourcegraph.com/docs/cody')
// Community and support
export const DISCORD_URL = new URL('https://discord.gg/s2qDtYGnAE')
export const CODY_FEEDBACK_URL = new URL('https://github.com/sourcegraph/cody/issues/new/choose')
export const CODY_SUPPORT_URL = new URL('https://help.sourcegraph.com/hc/en-us/requests/new')
// Account
export const ACCOUNT_UPGRADE_URL = new URL('https://sourcegraph.com/cody/subscription')
export const ACCOUNT_USAGE_URL = new URL('https://sourcegraph.com/cody/manage')
export const ACCOUNT_LIMITS_INFO_URL = new URL(
'https://sourcegraph.com/docs/cody/troubleshooting#autocomplete-rate-limits'
)
export const defaultAuthStatus = {
endpoint: '',
isDotCom: true,
isLoggedIn: false,
showInvalidAccessTokenError: false,
authenticated: false,
hasVerifiedEmail: false,
requiresVerifiedEmail: false,
siteHasCodyEnabled: false,
siteVersion: '',
userCanUpgrade: false,
username: '',
primaryEmail: '',
displayName: '',
avatarURL: '',
} satisfies AuthStatus
export const unauthenticatedStatus = {
endpoint: '',
isDotCom: true,
isLoggedIn: false,
showInvalidAccessTokenError: true,
authenticated: false,
hasVerifiedEmail: false,
requiresVerifiedEmail: false,
siteHasCodyEnabled: false,
siteVersion: '',
userCanUpgrade: false,
username: '',
primaryEmail: '',
displayName: '',
avatarURL: '',
} satisfies AuthStatus
export const networkErrorAuthStatus = {
isDotCom: false,
showInvalidAccessTokenError: false,
authenticated: false,
isLoggedIn: false,
hasVerifiedEmail: false,
showNetworkError: true,
requiresVerifiedEmail: false,
siteHasCodyEnabled: false,
siteVersion: '',
userCanUpgrade: false,
username: '',
primaryEmail: '',
displayName: '',
avatarURL: '',
} satisfies Omit<AuthStatus, 'endpoint'>
/** The local environment of the editor. */
export interface LocalEnv {
// The operating system kind
os: string
arch: string
homeDir?: string | undefined
extensionVersion: string
// Whether the extension is running in VS Code Web (as opposed to VS Code Desktop).
uiKindIsWeb: boolean
}
export function isLoggedIn(authStatus: AuthStatus): boolean {
if (!authStatus.siteHasCodyEnabled) {
return false
}
return (
authStatus.authenticated &&
(authStatus.requiresVerifiedEmail ? authStatus.hasVerifiedEmail : true)
)
}
export type AuthMethod = 'dotcom' | 'github' | 'gitlab' | 'google'
// Provide backward compatibility for the old token regex
// Details: https://docs.sourcegraph.com/dev/security/secret_formats
const sourcegraphTokenRegex =
/(sgp_(?:[a-fA-F0-9]{16}|local)|sgp_)?[a-fA-F0-9]{40}|(sgd|slk|sgs)_[a-fA-F0-9]{64}/
/**
* Checks if the given text matches the regex for a Sourcegraph access token.
*
* @param text - The text to check against the regex.
* @returns Whether the text matches the Sourcegraph token regex.
*/
export function isSourcegraphToken(text: string): boolean {
return sourcegraphTokenRegex.test(text)
}