- Notifications
You must be signed in to change notification settings - Fork35
feat: add support for coder inbox#444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
15 commits Select commitHold shift + click to select a range
f73eaee feat: begin impl of coder inbox
DanielleMaywood2de7f12 chore: take inspiration from existing websocket
DanielleMaywood88b31a2 chore: apply feedback
DanielleMaywood1809fb8 chore: update websocket url
DanielleMaywoodeae596e chore: replace showWarningMessage with showInformationMessage
DanielleMaywood5f4d8e8 Merge branch 'main' into dm-coder-inbox-oom-ood
DanielleMaywoodbe3f0a4 chore: upgrade dependencies
DanielleMaywood455ba73 Merge branch 'main' into dm-coder-inbox-oom-ood
DanielleMaywoodf4f99b2 chore: only upgrade coder dependency
DanielleMaywood57acbff chore: socketUrlRaw -> socketUrl
DanielleMaywood48d1c6b chore: private -> #
DanielleMaywood7f7d9c0 chore: cleanup on websocket error
DanielleMaywoodcf5ebea chore: add comment
DanielleMaywood474b906 chore: appease the linter
DanielleMaywoodd695588 chore: request plaintext format from api
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletionsrc/api.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletionssrc/inbox.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { Api } from "coder/site/src/api/api" | ||
| import { Workspace, GetInboxNotificationResponse } from "coder/site/src/api/typesGenerated" | ||
| import { ProxyAgent } from "proxy-agent" | ||
| import * as vscode from "vscode" | ||
| import { WebSocket } from "ws" | ||
| import { errToStr } from "./api-helper" | ||
| import { type Storage } from "./storage" | ||
| // These are the template IDs of our notifications. | ||
| // Maybe in the future we should avoid hardcoding | ||
| // these in both coderd and here. | ||
| const TEMPLATE_WORKSPACE_OUT_OF_MEMORY = "a9d027b4-ac49-4fb1-9f6d-45af15f64e7a" | ||
| const TEMPLATE_WORKSPACE_OUT_OF_DISK = "f047f6a3-5713-40f7-85aa-0394cce9fa3a" | ||
| export class Inbox implements vscode.Disposable { | ||
| readonly #storage: Storage | ||
| #disposed = false | ||
| #socket: WebSocket | ||
| constructor(workspace: Workspace, httpAgent: ProxyAgent, restClient: Api, storage: Storage) { | ||
| this.#storage = storage | ||
| const baseUrlRaw = restClient.getAxiosInstance().defaults.baseURL | ||
| if (!baseUrlRaw) { | ||
| throw new Error("No base URL set on REST client") | ||
| } | ||
| const watchTemplates = [TEMPLATE_WORKSPACE_OUT_OF_DISK, TEMPLATE_WORKSPACE_OUT_OF_MEMORY] | ||
| const watchTemplatesParam = encodeURIComponent(watchTemplates.join(",")) | ||
| const watchTargets = [workspace.id] | ||
| const watchTargetsParam = encodeURIComponent(watchTargets.join(",")) | ||
| // We shouldn't need to worry about this throwing. Whilst `baseURL` could | ||
| // be an invalid URL, that would've caused issues before we got to here. | ||
| const baseUrl = new URL(baseUrlRaw) | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| const socketProto = baseUrl.protocol === "https:" ? "wss:" : "ws:" | ||
| const socketUrl = `${socketProto}//${baseUrl.host}/api/v2/notifications/inbox/watch?format=plaintext&templates=${watchTemplatesParam}&targets=${watchTargetsParam}` | ||
| const coderSessionTokenHeader = "Coder-Session-Token" | ||
| this.#socket = new WebSocket(new URL(socketUrl), { | ||
| followRedirects: true, | ||
| agent: httpAgent, | ||
| headers: { | ||
| [coderSessionTokenHeader]: restClient.getAxiosInstance().defaults.headers.common[coderSessionTokenHeader] as | ||
| | string | ||
| | undefined, | ||
| }, | ||
| }) | ||
| this.#socket.on("open", () => { | ||
| this.#storage.writeToCoderOutputChannel("Listening to Coder Inbox") | ||
| }) | ||
| this.#socket.on("error", (error) => { | ||
| this.notifyError(error) | ||
| this.dispose() | ||
| }) | ||
| this.#socket.on("message", (data) => { | ||
| try { | ||
| const inboxMessage = JSON.parse(data.toString()) as GetInboxNotificationResponse | ||
| vscode.window.showInformationMessage(inboxMessage.notification.title) | ||
| } catch (error) { | ||
| this.notifyError(error) | ||
| } | ||
Parkreiner marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| }) | ||
| } | ||
| dispose() { | ||
| if (!this.#disposed) { | ||
| this.#storage.writeToCoderOutputChannel("No longer listening to Coder Inbox") | ||
| this.#socket.close() | ||
| this.#disposed = true | ||
| } | ||
| } | ||
| private notifyError(error: unknown) { | ||
| const message = errToStr(error, "Got empty error while monitoring Coder Inbox") | ||
| this.#storage.writeToCoderOutputChannel(message) | ||
| } | ||
| } | ||
8 changes: 7 additions & 1 deletionsrc/remote.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -9,12 +9,13 @@ import * as path from "path" | ||
| import prettyBytes from "pretty-bytes" | ||
| import * as semver from "semver" | ||
| import * as vscode from "vscode" | ||
| import {createHttpAgent,makeCoderSdk, needToken, startWorkspaceIfStoppedOrFailed, waitForBuild } from "./api" | ||
| import { extractAgents } from "./api-helper" | ||
| import * as cli from "./cliManager" | ||
| import { Commands } from "./commands" | ||
| import { featureSetForVersion, FeatureSet } from "./featureSet" | ||
| import { getHeaderCommand } from "./headers" | ||
| import { Inbox } from "./inbox" | ||
| import { SSHConfig, SSHValues, mergeSSHConfigValues } from "./sshConfig" | ||
| import { computeSSHProperties, sshSupportsSetEnv } from "./sshSupport" | ||
| import { Storage } from "./storage" | ||
| @@ -403,6 +404,11 @@ export class Remote { | ||
| disposables.push(monitor) | ||
| disposables.push(monitor.onChange.event((w) => (this.commands.workspace = w))) | ||
| // Watch coder inbox for messages | ||
| const httpAgent = await createHttpAgent() | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| const inbox = new Inbox(workspace, httpAgent, workspaceRestClient, this.storage) | ||
| disposables.push(inbox) | ||
| // Wait for the agent to connect. | ||
| if (agent.status === "connecting") { | ||
| this.storage.writeToCoderOutputChannel(`Waiting for ${workspaceName}/${agent.name}...`) | ||
2 changes: 1 addition & 1 deletionyarn.lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.