Skip to content

Commit

Permalink
experimental support for Cody as a VS Code Web extension (#424)
Browse files Browse the repository at this point in the history
Introduces a few "platform" abstractions to make it possible to run Cody
as a VS Code Web extension (i.e., running in the web browser at
https://vscode.dev or similar).

Web extension usage is for internal development and testing only; it is
not supported and may be removed at any time.

See `vscode/doc/web.md` for information about this experimental,
unsupported method for running Cody in VS Code Web.

## Test plan

Run `cd vscode && pnpm run watch:dev:web` then open
http://localhost:3000 in your web browser. You should see the Cody
extension in VS Code Web.
  • Loading branch information
sqs authored Jul 28, 2023
1 parent 52b465a commit ae16c27
Show file tree
Hide file tree
Showing 19 changed files with 557 additions and 153 deletions.
32 changes: 28 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"version": "0.1.0",
"configurations": [
{
"name": "Launch VS Code Extension",
"name": "Launch VS Code Extension (Desktop)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"preLaunchTask": "Build VS Code Extension",
"preLaunchTask": "Build VS Code Extension (Desktop)",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}/vscode",
"--disable-extension=sourcegraph.cody-ai",
Expand All @@ -21,11 +21,11 @@
}
},
{
"name": "Launch VS Code Extension (Separate Instance)",
"name": "Launch VS Code Extension (Desktop; Separate Instance)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"preLaunchTask": "Build VS Code Extension",
"preLaunchTask": "Build VS Code Extension (Desktop)",
"args": [
"--user-data-dir=/tmp/vscode-cody-extension-dev-host",
"--profile-temp",
Expand All @@ -42,6 +42,30 @@
"CODY_DEBUG_ENABLE": "true",
"CODY_FOCUS_ON_STARTUP": "1"
}
},
{
"name": "Launch VS Code Extension (Web, in Browser)",
"type": "node",
"request": "launch",
"preLaunchTask": "Build VS Code Extension (Web)",
"runtimeExecutable": "pnpm",
"runtimeArgs": ["-C", "${workspaceFolder}/vscode", "run", "--silent", "_dev:vscode-test-web"],
"outFiles": ["${workspaceFolder}/vscode/dist/**/*.js"]
},
{
"name": "Launch VS Code Extension (Web Extension Host)",
"type": "pwa-extensionHost",
"debugWebWorkerHost": true,
"request": "launch",
"preLaunchTask": "Build VS Code Extension (Web)",
"outFiles": ["${workspaceFolder}/vscode/dist/**/*.js"],
"args": [
"--extensionDevelopmentPath=${workspaceRoot}/vscode",
"--extensionDevelopmentKind=web",
"--disable-extension=sourcegraph.cody-ai",
"--disable-extension=github.copilot",
"--disable-extension=github.copilot-nightly"
]
}
]
}
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"search.exclude": {
"**/node_modules": true,
"**/dist": true
"**/dist": true,
"**/.eslintcache": true,
"**/.vscode-test": true,
"**/.vscode-test-web": true
},
"files.exclude": {
"**/node_modules": true,
"**/dist": true,
"**/.eslintcache": true,
"**/.vscode-test": true
"**/.vscode-test": true,
"**/.vscode-test-web": true
},
"editor.formatOnSave": true,
"npm.packageManager": "pnpm",
Expand Down
13 changes: 11 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@
}
},
{
"label": "Build VS Code Extension",
"label": "Build VS Code Extension (Desktop)",
"type": "npm",
"path": "vscode",
"script": "build:dev",
"script": "build:dev:desktop",
"problemMatcher": "$tsc-watch",
"options": { "cwd": "vscode" },
"isBackground": true
},
{
"label": "Build VS Code Extension (Web)",
"type": "npm",
"path": "vscode",
"script": "build:dev:web",
"problemMatcher": "$tsc-watch",
"options": { "cwd": "vscode" },
"isBackground": true
Expand Down
2 changes: 1 addition & 1 deletion doc/dev/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

### Build and run the VS Code extension

Open this repository in VS Code and run the `Launch VS Code Extension` build/debug task (or run `pnpm -C vscode run dev`).
Open this repository in VS Code and run the `Launch VS Code Extension (Desktop)` build/debug task (or run `pnpm -C vscode run dev`).

See [vscode/CONTRIBUTING.md](../../vscode/CONTRIBUTING.md) for more information.

Expand Down
10 changes: 10 additions & 0 deletions lib/shared/src/sourcegraph-api/completions/browserClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class SourcegraphBrowserCompletionsClient extends SourcegraphCompletionsC
headers: Object.fromEntries(headersInstance.entries()),
body: JSON.stringify(params),
signal: abort.signal,
openWhenHidden: isRunningInWebWorker, // otherwise tries to call document.addEventListener
async onopen(response) {
if (!response.ok && response.headers.get('content-type') !== 'text/event-stream') {
let errorMessage: null | string = null
Expand Down Expand Up @@ -59,3 +60,12 @@ export class SourcegraphBrowserCompletionsClient extends SourcegraphCompletionsC
}
}
}

declare const WorkerGlobalScope: never
const isRunningInWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope

if (isRunningInWebWorker) {
// HACK: @microsoft/fetch-event-source tries to call document.removeEventListener, which is not
// available in a worker.
;(self as any).document = { removeEventListener: () => {} }
}
Loading

0 comments on commit ae16c27

Please sign in to comment.