Skip to content

Commit

Permalink
Agent: Changes required for running webview in clients (#5157)
Browse files Browse the repository at this point in the history
Context:
https://sourcegraph.slack.com/archives/C06R69BC8UW/p1723199277787079

## Changes

A bunch of changes needed for getting chat webview to work in other
clients, e.g JetBrains, Visual Studio and Eclipse.

Related PR: sourcegraph/jetbrains#2022

## Test plan

Full QA on jetbrains branch

---------

Co-authored-by: Dominic Cooney <[email protected]>
Co-authored-by: Beatrix <[email protected]>
Co-authored-by: Beatrix <[email protected]>
Co-authored-by: Daniel Marques <[email protected]>
  • Loading branch information
5 people authored Aug 10, 2024
1 parent 869de99 commit 9ded843
Show file tree
Hide file tree
Showing 44 changed files with 1,187 additions and 299 deletions.
10 changes: 10 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
hoist=false
1
2
3
4
5
6
7
8
9
10
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
nodejs 20.4.0
pnpm 8.6.7
yarn 1.22.19 # required for agent/scripts/generate-agent-bindings.sh -> scip-typescript-cody-bindings
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ data class ClientCapabilities(
val ignore: IgnoreEnum? = null, // Oneof: none, enabled
val codeActions: CodeActionsEnum? = null, // Oneof: none, enabled
val webviewMessages: WebviewMessagesEnum? = null, // Oneof: object-encoded, string-encoded
val webview: WebviewEnum? = null, // Oneof: agentic, native
val webviewNativeConfig: WebviewNativeConfigParams? = null,
) {

enum class CompletionsEnum {
Expand Down Expand Up @@ -82,5 +84,10 @@ data class ClientCapabilities(
@SerializedName("object-encoded") `Object-encoded`,
@SerializedName("string-encoded") `String-encoded`,
}

enum class WebviewEnum {
@SerializedName("agentic") Agentic,
@SerializedName("native") Native,
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ interface CodyAgentClient {
fun textDocument_show(params: TextDocument_ShowParams): CompletableFuture<Boolean>
@JsonRequest("workspace/edit")
fun workspace_edit(params: WorkspaceEditParams): CompletableFuture<Boolean>
@JsonRequest("webview/create")
fun webview_create(params: Webview_CreateParams): CompletableFuture<Null?>
@JsonRequest("env/openExternal")
fun env_openExternal(params: Env_OpenExternalParams): CompletableFuture<Boolean>

// =============
// Notifications
Expand All @@ -48,4 +48,20 @@ interface CodyAgentClient {
fun remoteRepo_didChange(params: Null?)
@JsonNotification("remoteRepo/didChangeState")
fun remoteRepo_didChangeState(params: RemoteRepoFetchState)
@JsonNotification("webview/registerWebviewViewProvider")
fun webview_registerWebviewViewProvider(params: Webview_RegisterWebviewViewProviderParams)
@JsonNotification("webview/createWebviewPanel")
fun webview_createWebviewPanel(params: Webview_CreateWebviewPanelParams)
@JsonNotification("webview/dispose")
fun webview_dispose(params: Webview_DisposeParams)
@JsonNotification("webview/reveal")
fun webview_reveal(params: Webview_RevealParams)
@JsonNotification("webview/setTitle")
fun webview_setTitle(params: Webview_SetTitleParams)
@JsonNotification("webview/setIconPath")
fun webview_setIconPath(params: Webview_SetIconPathParams)
@JsonNotification("webview/setOptions")
fun webview_setOptions(params: Webview_SetOptionsParams)
@JsonNotification("webview/setHtml")
fun webview_setHtml(params: Webview_SetHtmlParams)
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ interface CodyAgentServer {
fun git_codebaseName(params: Git_CodebaseNameParams): CompletableFuture<String?>
@JsonRequest("webview/didDispose")
fun webview_didDispose(params: Webview_DidDisposeParams): CompletableFuture<Null?>
@JsonRequest("webview/resolveWebviewView")
fun webview_resolveWebviewView(params: Webview_ResolveWebviewViewParams): CompletableFuture<Null?>
@JsonRequest("webview/receiveMessageStringEncoded")
fun webview_receiveMessageStringEncoded(params: Webview_ReceiveMessageStringEncodedParams): CompletableFuture<Null?>
@JsonRequest("diagnostics/publish")
Expand Down Expand Up @@ -168,4 +170,6 @@ interface CodyAgentServer {
fun autocomplete_completionAccepted(params: CompletionItemParams)
@JsonNotification("progress/cancel")
fun progress_cancel(params: Progress_CancelParams)
@JsonNotification("webview/didDisposeNative")
fun webview_didDisposeNative(params: Webview_DidDisposeNativeParams)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ object Constants {
const val Pending = "Pending"
const val Working = "Working"
const val accuracy = "accuracy"
const val agentic = "agentic"
const val ask = "ask"
const val assistant = "assistant"
const val autocomplete = "autocomplete"
Expand Down Expand Up @@ -53,6 +54,7 @@ object Constants {
const val isChatErrorGuard = "isChatErrorGuard"
const val local = "local"
const val method = "method"
const val native = "native"
const val none = "none"
const val notification = "notification"
const val `object-encoded` = "object-encoded"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class DefiniteWebviewOptions(
val enableScripts: Boolean,
val enableForms: Boolean,
val enableOnlyCommandUris: List<String>? = null,
val localResourceRoots: List<String>? = null,
val portMapping: List<PortMappingParams>,
val enableFindWidget: Boolean,
val retainContextWhenHidden: Boolean,
)

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_CreateParams(
val id: String,
val data: Any,
data class Env_OpenExternalParams(
val uri: String,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class PortMappingParams(
val webviewPort: Long,
val extensionHostPort: Long,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class ShowOptionsParams(
val preserveFocus: Boolean,
val viewColumn: Long,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class WebviewCreateWebviewPanelOptions(
val enableScripts: Boolean,
val enableForms: Boolean,
val enableOnlyCommandUris: List<String>? = null,
val localResourceRoots: List<String>? = null,
val portMapping: List<PortMappingParams>,
val enableFindWidget: Boolean,
val retainContextWhenHidden: Boolean,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class WebviewNativeConfigParams(
val cspSource: String,
val webviewBundleServingPrefix: String,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_CreateWebviewPanelParams(
val handle: String,
val viewType: String,
val title: String,
val showOptions: ShowOptionsParams,
val options: WebviewCreateWebviewPanelOptions,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_DidDisposeNativeParams(
val handle: String,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_DisposeParams(
val handle: String,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_RegisterWebviewViewProviderParams(
val viewId: String,
val retainContextWhenHidden: Boolean,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_ResolveWebviewViewParams(
val viewId: String,
val webviewHandle: String,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_RevealParams(
val handle: String,
val viewColumn: Long,
val preserveFocus: Boolean,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_SetHtmlParams(
val handle: String,
val html: String,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_SetIconPathParams(
val handle: String,
val iconPathUri: String? = null,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_SetOptionsParams(
val handle: String,
val options: DefiniteWebviewOptions,
)

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:Suppress("FunctionName", "ClassName", "unused", "EnumEntryName", "UnusedImport")
package com.sourcegraph.cody.agent.protocol_generated;

data class Webview_SetTitleParams(
val handle: String,
val title: String,
)

31 changes: 29 additions & 2 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"scripts": {
"build:root": "pnpm -C .. run -s build",
"build:agent": "node src/esbuild.mjs",
"build": "pnpm run -s build:root && pnpm run -s build:agent",
"build:webviews": "pnpm -C ../vscode _build:webviews --mode production && cp -R ../vscode/dist/webviews dist/webviews",
"build": "pnpm run -s build:root && pnpm run -s build:webviews && pnpm run -s build:agent",
"build-minify": "pnpm run build --minify",
"agent": "pnpm run build && node --enable-source-maps dist/index.js",
"agent:skip-root-build": "pnpm run build:agent && node --enable-source-maps dist/index.js",
Expand All @@ -24,7 +25,13 @@
"prepublishOnly": "pnpm run build"
},
"bin": "dist/index.js",
"files": ["dist/index.js", "dist/index.js.map", "dist/*.wasm", "dist/win-ca-roots.exe"],
"files": [
"dist/index.js",
"dist/index.js.map",
"dist/*.wasm",
"dist/win-ca-roots.exe",
"dist/webviews/*"
],
"peerDependencies": {
"@inquirer/prompts": "^5.0.7",
"@pollyjs/core": "^6.0.6",
Expand Down Expand Up @@ -60,19 +67,39 @@
"@types/fast-json-stable-stringify": "^2.1.0",
"@types/glob": "^8.0.0",
"@types/google-protobuf": "3.15.12",
"@types/js-levenshtein": "^1.1.1",
"@types/lodash": "^4.14.195",
"@types/minimatch": "^5.1.2",
"@types/open": "^6.2.1",
"@types/uuid": "^9.0.2",
"@types/vscode": "^1.80.0",
"@types/win-ca": "^3.5.4",
"@types/ws": "^8.5.10",
"chalk": "^5.3.0",
"commander": "^11.1.0",
"csv-writer": "^1.6.0",
"dedent": "^0.7.0",
"diff": "^5.2.0",
"easy-table": "^1.2.0",
"env-paths": "^3.0.0",
"esbuild": "^0.18.19",
"fast-json-stable-stringify": "^2.1.0",
"fast-myers-diff": "^3.2.0",
"glob": "^7.2.3",
"google-protobuf": "^3.21.2",
"js-levenshtein": "^1.1.6",
"lodash": "^4.17.21",
"mac-ca": "^2.0.3",
"minimatch": "^9.0.3",
"open": "^8.4.2",
"ora": "^8.0.1",
"parse-git-diff": "^0.0.14",
"pretty-ms": "^8.0.0",
"rimraf": "^5.0.5",
"uuid": "^9.0.0",
"vscode-uri": "^3.0.7",
"win-ca": "^3.5.1",
"ws": "^8.16.0",
"yaml": "^2.3.4"
}
}
2 changes: 1 addition & 1 deletion agent/scripts/generate-agent-kotlin-bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pushd $INDEXER_DIR
git fetch origin
git checkout olafurpg/signatures-rebase1
git pull origin olafurpg/signatures-rebase1
yarn --ignore-engines install
yarn install
popd

pnpm install --prefer-offline
Expand Down
7 changes: 6 additions & 1 deletion agent/src/AgentWebviewPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import type { ExtensionMessage, WebviewMessage } from '../../vscode/src/chat/pro
import type { Repo } from '../../vscode/src/context/repo-fetcher'
import { EventEmitter, defaultWebviewPanel } from './vscode-shim'

/** Utility class to manage a list of `AgentWebPanel` instances. */
/** Utility class to manage a list of `AgentWebPanel` or "native" WebviewPanel instances. */
export class AgentWebviewPanels {
public panels = new Map<string, AgentWebviewPanel>()
// TODO: If we don't create AgentWebviewPanels when using native webviews, untangle nativePanels from this type.
public readonly nativePanels = new Map<
string,
{ didReceiveMessage: (message: any) => void; didDispose: () => void }
>()
public add(panel: AgentWebviewPanel): void {
this.panels.set(panel.panelID, panel)
}
Expand Down
Loading

0 comments on commit 9ded843

Please sign in to comment.