Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agent: support unstable-codegen access via SOCKS proxy #836

Merged
merged 24 commits into from
Sep 11, 2023
Merged

Conversation

cbart
Copy link
Contributor

@cbart cbart commented Aug 28, 2023

Part of sourcegraph/sourcegraph#56254

Add cody.proxy that makes agent connect with a SOCKS proxy for unstable-codegen.

This change paired with sourcegraph/sourcegraph#56264 routes autocomplete LLM traffic through a SOCKS proxy by extending ~/.sourcegraph-jetbrains.properties :

cody.proxy: socks5://127.0.0.1:9999

For VSCode the setting is not shown in the UI, not suggested as an autocomplete option in settings.json, and when typed manually, the deprecation message is shown (#836 (comment)).

Screenshot 2023-09-07 at 10 34 48

Test plan

  • Manually tested with IntelliJ + agent
  • Manually tested with VS Code (using http.proxy as described by the config description)

@cbart cbart requested review from a team August 28, 2023 11:00
@cbart
Copy link
Contributor Author

cbart commented Aug 28, 2023

I think I might be a little confused with dependency management here. Do I need to (and if so how do I) add transitive (?) dependencies of newly added socks-proxy?

➜  cody git:(cb/s/56254) pnpm -C vscode run build

> cody-ai@0.8.0 build /Users/cbart/cody/vscode
> tsc --build && pnpm run -s _build:esbuild:desktop && pnpm run -s _build:esbuild:web && pnpm run -s _build:webviews --mode production


  dist/extension.node.js      6.1mb ⚠️
  dist/extension.node.js.map  9.8mb

⚡ Done in 160ms
✘ [ERROR] Could not resolve "dns"

    ../node_modules/.pnpm/socks-proxy-agent@8.0.1/node_modules/socks-proxy-agent/dist/index.js:33:33:
      33 │ const dns = __importStar(require("dns"));
         ╵                                  ~~~~~

  The package "dns" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "--platform=node" to do that, which will remove this error.

✘ [ERROR] Could not resolve "net"

    ../node_modules/.pnpm/socks-proxy-agent@8.0.1/node_modules/socks-proxy-agent/dist/index.js:34:33:
      34 │ const net = __importStar(require("net"));
         ╵                                  ~~~~~

  The package "net" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "--platform=node" to do that, which will remove this error.

✘ [ERROR] Could not resolve "tls"

    ../node_modules/.pnpm/socks-proxy-agent@8.0.1/node_modules/socks-proxy-agent/dist/index.js:35:33:
      35 │ const tls = __importStar(require("tls"));
         ╵                                  ~~~~~

  The package "tls" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "--platform=node" to do that, which will remove this error.

✘ [ERROR] Could not resolve "http"

    ../node_modules/.pnpm/agent-base@7.1.0/node_modules/agent-base/dist/index.js:30:34:
      30 │ const http = __importStar(require("http"));
         ╵                                   ~~~~~~

  The package "http" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "--platform=node" to do that, which will remove this error.

✘ [ERROR] Could not resolve "http"

    ../node_modules/.pnpm/agent-base@7.1.0/node_modules/agent-base/dist/helpers.js:27:34:
      27 │ const http = __importStar(require("http"));
         ╵                                   ~~~~~~

  The package "http" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "--platform=node" to do that, which will remove this error.

✘ [ERROR] Could not resolve "https"

    ../node_modules/.pnpm/agent-base@7.1.0/node_modules/agent-base/dist/helpers.js:28:35:
      28 │ const https = __importStar(require("https"));
         ╵                                    ~~~~~~~

  The package "https" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "--platform=node" to do that, which will remove this error.

6 of 12 errors shown (disable the message limit with --log-limit=0)
 ELIFECYCLE  Command failed with exit code 1.

@philipp-spiess
Copy link
Contributor

Nice! This is the same "solution" we came up with for the VS Code extension 🙃 https://github.com/sourcegraph/sourcegraph/pull/42802/files

I wonder if we should make this config extension wide (e.g. cody.proxy or so)? We just need to make sure we pass it to all fetch() calls but we don't have to do that immediatly

@philipp-spiess
Copy link
Contributor

DMed you this as well but posting it here for completeness too.

The CI issues come beacuse socks is a node only library but VS Code extensions are generic to the underlying platform so they need to work on both node (desktop) and web (browser).

To fix this, we'll need to make sure to only include this library in the desktop build. We currently split based on these two entry files:

So you need to make sure that the code for this is injected at these endpoints and stubbed out for web.

@philipp-spiess
Copy link
Contributor

@cbart I've done the branching now with a mutable ref object pattern to enable keeping TCP connections alive. I think if you rebase your change on top of this PR, it should be relatively easy for you to fix the CI issue you are seeing: #868

@olafurpg olafurpg changed the title Agent supports unstable-codegen access via SOCKS proxy Agent: support unstable-codegen access via SOCKS proxy Sep 6, 2023
@taras-yemets taras-yemets marked this pull request as ready for review September 6, 2023 12:00
@taras-yemets taras-yemets requested a review from a team September 6, 2023 12:16
@@ -154,6 +154,7 @@ export interface ExtensionConfiguration {
customHeaders: Record<string, string>
autocompleteAdvancedProvider?: string
autocompleteAdvancedServerEndpoint?: string | null
autocompleteAdvancedServerSocksProxy?: string | null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this probably be better as a Proxy or AgentProxy setting in the current use we need it only for a socks proxy and only for autocomplete, but if a customer were to need it, I would think it's likely to need it for all network requests.

Copy link
Contributor

@taras-yemets taras-yemets Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. In this case we use autocompleteAdvancedServerSocksProxy for autocompleteAdvancedServerEndpoint. serverEndpoint might have its own proxy. What do you think about having proxies as Record<string, string where we can map endpoints to proxies, e.g.

proxies: {
  "https://server-endpoint.com": "https://server-endpoint-proxy.com",
  "https://autocomplete-endpoint.com": "socks://your-name%40gmail.com:abcdef12345124@br41.nordvpn.com"
}

Then in fetch.node.ts we can check whether there is a proxy for the requested URL and return the corresponding proxy agent. For example, if the proxy endpoint protocol is socks, we return the socks proxy agent instance.
cc: @chwarwick @philipp-spiess

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taras-yemets I don't think that's something that would be common. VS Code's proxy setting also only allows one proxy server. If they really need to have a complicated setup like this, I imagine that they would configure this on the proxy level?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to "proxy" 01f9c04

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corresponding change in the paired PR: sourcegraph/sourcegraph@22f0dde

@@ -869,6 +869,10 @@
"type": "string",
"markdownDescription": "The server endpoint used for code autocomplete. This is only supported with a provider other than `anthropic`."
},
"cody.autocomplete.advanced.serverSocksProxy": {
"type": "string",
"markdownDeprecationMessage": "The SOCKS proxy endpoint to access server endpoint. This is only supported with `unstable-codegen` provider and only for use with the Cody Agent (for instance with JetBrains plugin). For VS Code please use http.proxy instead."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels wrong to add a new setting to the settings UI that is marked as Deprecated but in reality it isn't. Is it possible to not display this setting in the settings UI? I feel like there used to settings that were could only be set via the json and this I think would be a better user experience.

Copy link
Contributor

@taras-yemets taras-yemets Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we provide markdownDeprecationMessage this setting is not visible in the settings UI (docs). This setting is:

  • not shown in settings UI
Screenshot 2023-09-07 at 10 33 11
  • not suggested in autocomplete options in settings.json
Screenshot 2023-09-07 at 10 36 02
  • can be typed manually with the deprecation message shown
Screenshot 2023-09-07 at 10 34 48

Updated the description.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice the picture in the description was what got me.

@slimsag
Copy link
Member

slimsag commented Sep 7, 2023

Nice work on bringing this to the finish line @taras-yemets :D

vscode/src/fetch.node.ts Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants