forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathextension.ts
More file actions
243 lines (209 loc) · 9.04 KB
/
extension.ts
File metadata and controls
243 lines (209 loc) · 9.04 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
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
import * as vscode from "vscode"
import * as dotenvx from "@dotenvx/dotenvx"
import delay from "delay"
import * as path from "path"
// Load environment variables from .env file
try {
// Specify path to .env file in the project root directory
const envPath = path.join(__dirname, "..", ".env")
dotenvx.config({ path: envPath })
} catch (e) {
// Silently handle environment loading errors
console.warn("Failed to load environment variables:", e)
}
import "./utils/path" // Necessary to have access to String.prototype.toPosix.
import { initializeI18n } from "./i18n"
import { ContextProxy } from "./core/config/ContextProxy"
import { ClineProvider } from "./core/webview/ClineProvider"
import { CodeActionProvider } from "./core/CodeActionProvider"
import { DIFF_VIEW_URI_SCHEME } from "./integrations/editor/DiffViewProvider"
import { McpServerManager } from "./services/mcp/McpServerManager"
import { telemetryService } from "./services/telemetry/TelemetryService"
import { TerminalRegistry } from "./integrations/terminal/TerminalRegistry"
import { API } from "./exports/api"
import { migrateSettings } from "./utils/migrateSettings"
import {
handleUri,
registerCommands,
registerCodeActions,
registerTerminalActions,
registerPearListener,
} from "./activate"
import { formatLanguage } from "./shared/language"
/**
* Built using https://github.com/microsoft/vscode-webview-ui-toolkit
*
* Inspired by:
* - https://github.com/microsoft/vscode-webview-ui-toolkit-samples/tree/main/default/weather-webview
* - https://github.com/microsoft/vscode-webview-ui-toolkit-samples/tree/main/frameworks/hello-world-react-cra
*/
let outputChannel: vscode.OutputChannel
let extensionContext: vscode.ExtensionContext
// This method is called when your extension is activated.
// Your extension is activated the very first time the command is executed.
export async function activate(context: vscode.ExtensionContext) {
extensionContext = context
outputChannel = vscode.window.createOutputChannel("Roo-Code")
context.subscriptions.push(outputChannel)
outputChannel.appendLine("Roo-Code extension activated")
// Migrate old settings to new
await migrateSettings(context, outputChannel)
// Initialize telemetry service after environment variables are loaded.
telemetryService.initialize()
// Initialize i18n for internationalization support
initializeI18n(context.globalState.get("language") ?? formatLanguage(vscode.env.language))
// Initialize terminal shell execution handlers.
TerminalRegistry.initialize()
// Get default commands from configuration.
const defaultCommands = vscode.workspace.getConfiguration("roo-cline").get<string[]>("allowedCommands") || []
// Initialize global state if not already set.
if (!context.globalState.get("allowedCommands")) {
context.globalState.update("allowedCommands", defaultCommands)
}
const contextProxy = await ContextProxy.getInstance(context)
const provider = new ClineProvider(context, outputChannel, "sidebar", contextProxy)
telemetryService.setProvider(provider)
registerPearListener(provider);
context.subscriptions.push(
vscode.window.registerWebviewViewProvider(ClineProvider.sideBarId, provider, {
webviewOptions: { retainContextWhenHidden: true },
}),
)
registerCommands({ context, outputChannel, provider })
/**
* We use the text document content provider API to show the left side for diff
* view by creating a virtual document for the original content. This makes it
* readonly so users know to edit the right side if they want to keep their changes.
*
* This API allows you to create readonly documents in VSCode from arbitrary
* sources, and works by claiming an uri-scheme for which your provider then
* returns text contents. The scheme must be provided when registering a
* provider and cannot change afterwards.
*
* Note how the provider doesn't create uris for virtual documents - its role
* is to provide contents given such an uri. In return, content providers are
* wired into the open document logic so that providers are always considered.
*
* https://code.visualstudio.com/api/extension-guides/virtual-documents
*/
context.subscriptions.push(
vscode.commands.registerCommand("pearai-roo-cline.pearaiLogin", async (data) => {
console.dir("Logged in to PearAI:")
console.dir(data)
context.secrets.store("pearaiApiKey", data.accessToken)
context.secrets.store("pearaiRefreshKey", data.refreshToken)
const provider = await ClineProvider.getInstance()
if (provider) {
// Update the API configuration to clear the PearAI key
await provider.setValues({
pearaiApiKey: data.accessToken,
})
await provider.postStateToWebview()
// Update MCP server with new token
const mcpHub = provider.getMcpHub()
if (mcpHub) {
await mcpHub.updatePearAIApiKey(data.accessToken)
}
}
vscode.commands.executeCommand("roo-cline.plusButtonClicked")
}),
)
context.subscriptions.push(
vscode.commands.registerCommand("pearai-roo-cline.updatePearAITokens", async (data) => {
console.dir("Updated PearAI tokens:")
console.dir(data)
context.secrets.store("pearaiApiKey", data.accessToken)
context.secrets.store("pearaiRefreshKey", data.refreshToken)
const provider = await ClineProvider.getInstance()
if (provider) {
// Update the API configuration to clear the PearAI key
await provider.setValues({
pearaiApiKey: data.accessToken,
})
await provider.postStateToWebview()
// Update MCP server with new token
const mcpHub = provider.getMcpHub()
if (mcpHub) {
await mcpHub.updatePearAIApiKey(data.accessToken)
}
}
}),
)
context.subscriptions.push(
vscode.commands.registerCommand("pearai-roo-cline.pearaiLogout", async () => {
console.dir("Logged out of PearAI:")
context.secrets.delete("pearaiApiKey")
context.secrets.delete("pearaiRefreshKey")
// Get the current provider instance and update webview state
const provider = await ClineProvider.getInstance()
if (provider) {
// Update the API configuration to clear the PearAI key
await provider.setValues({
pearaiApiKey: undefined,
})
await provider.postStateToWebview()
// Clear MCP server token
const mcpHub = provider.getMcpHub()
if (mcpHub) {
await mcpHub.clearPearAIApiKey()
}
}
}),
)
context.subscriptions.push(
vscode.commands.registerCommand("pearai-roo-cline.PearAIKeysNotFound", async () => {
const provider = await ClineProvider.getInstance()
if (provider) {
provider.postMessageToWebview({ type: "action", action: "PearAIKeysNotFound" })
}
}),
)
context.subscriptions.push(
vscode.commands.registerCommand("pearai-roo-cline.getPearAIApiKey", async () => {
return await context.secrets.get("pearaiApiKey")
}),
)
/*
We use the text document content provider API to show the left side for diff view by creating a virtual document for the original content. This makes it readonly so users know to edit the right side if they want to keep their changes.
- This API allows you to create readonly documents in VSCode from arbitrary sources, and works by claiming an uri-scheme for which your provider then returns text contents. The scheme must be provided when registering a provider and cannot change afterwards.
- Note how the provider doesn't create uris for virtual documents - its role is to provide contents given such an uri. In return, content providers are wired into the open document logic so that providers are always considered.
https://code.visualstudio.com/api/extension-guides/virtual-documents
*/
const diffContentProvider = new (class implements vscode.TextDocumentContentProvider {
provideTextDocumentContent(uri: vscode.Uri): string {
return Buffer.from(uri.query, "base64").toString("utf-8")
}
})()
context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(DIFF_VIEW_URI_SCHEME, diffContentProvider),
)
context.subscriptions.push(vscode.window.registerUriHandler({ handleUri }))
// Register code actions provider.
context.subscriptions.push(
vscode.languages.registerCodeActionsProvider({ pattern: "**/*" }, new CodeActionProvider(), {
providedCodeActionKinds: CodeActionProvider.providedCodeActionKinds,
}),
)
registerCodeActions(context)
registerTerminalActions(context)
context.subscriptions.push(
vscode.commands.registerCommand("roo-cline.focus", async (...args: any[]) => {
await vscode.commands.executeCommand("pearai-roo-cline.SidebarProvider.focus")
}),
)
// Allows other extensions to activate once Roo is ready.
vscode.commands.executeCommand("roo-cline.activationCompleted")
// Implements the `RooCodeAPI` interface.
const socketPath = process.env.ROO_CODE_IPC_SOCKET_PATH
const enableLogging = typeof socketPath === "string"
return new API(outputChannel, provider, socketPath, enableLogging)
}
// This method is called when your extension is deactivated
export async function deactivate() {
outputChannel.appendLine("Roo-Code extension deactivated")
// Clean up MCP server manager
await McpServerManager.cleanup(extensionContext)
telemetryService.shutdown()
// Clean up terminal handlers
TerminalRegistry.cleanup()
}